Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React passing a prop to components return undefined

I'm really confused as to why when I route to http://localhost:3000/subjects/physics of my project.

The variable gradeSelection is defined in App.js state. It is passed to subjectCards.js component via props as gradeSelection, which passes it onto a Subject.js component via props as gradeSelection.

However, this.props.gradeSelection in Subjects.js returns undefined.

Is there something I might possibly be doing wrong?

Console output:

App.js: Year 12             // correct
subjectCards.js: Year 12    // correct
Subject.js: undefined       // not correct

App.js

constructor(props) {
  super(props);
  this.state = {
    gradeSelection: "Year 12"
  };
}

render() {
  console.log("App: "+this.state.gradeSelection)
  return (
    <Route path="/subjects" render={(props)=>(<SubjectCards {...props}  gradeSelection={this.state.gradeSelection} />)} />
);


}

subjectCards.js

let display;

console.log("subjectCards.js: "+props.gradeSelection)
display = <Route path="/subjects/:subjectName" render={(props)=><Subject {...props} gradeSelection={props.gradeSelection}/>} />


return (
  display
);

Subject.js

constructor(props) {
  super(props);
  console.log("Subject.js: "+this.props.gradeSelection);  // undefined
}

Thank you!

EDIT:

When console.log(props) or console.log(this.props) in Subjects.js constructor. gradeSelection inside the console output is still undefined..

I've tried passing a string to gradeSelection in subjectCards.js and the console output was correct in returning the string in Subject.js..

display = <Route path="/subjects/:subjectName" render={(props)=><Subject {...props} gradeSelection={"props.gradeSelection"}/>} />
like image 958
tom_son Avatar asked Nov 22 '18 02:11

tom_son


People also ask

Why props is coming undefined In React?

The "Cannot read property 'props' of undefined" error occurs when a class method is called without having the correct context bound to the this keyword. To solve the error, define the class method as an arrow function or use the bind method in the classes' constructor method.

Can I return undefined In React component?

We cannot include undefined because it would throw an error at runtime.

How do you fix an undefined error in React?

The Quick Fix This error usually means you're trying to use . map on an array, but that array isn't defined yet. That's often because the array is a piece of undefined state or an undefined prop. Make sure to initialize the state properly.

Why is Prop value undefined?

The "cannot set property 'props' of undefined" error occurs when we add an extra set of parenthesis when declaring a class component in React. js. To solve the error, remove the parenthesis after Component in your class declaration, e.g. class App extends Component {} . Here is an example of how the error occurs.


1 Answers

Without seeing the rest of your code, I'm going to assume that subjectCards.js is a functional component that looks like this. If it's not, could you please post the complete component?

function SubjectCards(props) {
  let display

  console.log('subjectCards.js: ' + props.gradeSelection)

  display = (
    <Route
      path="/subjects/:subjectName"
      render={props => (
        <Subject {...props} gradeSelection={props.gradeSelection} />
      )}
    />
  )

  return display
}

What I'm seeing wrong with this code in your particular use case is that on line 1, you have an argument with the name of props. If you follow the code down to line 9, you'll notice that the anonymous function call inside render also has a props argument. On line 10, you're calling props.gradeSelection which would look inside the argument found on line 9 and not the argument found on line 1, giving you undefined.

There are a couple different ways of fixing this. One way I'd recommend is destructuring your argument props on line 1.

function SubjectCards({ gradeSelection }) { // See how we went from props to {gradeSelection}
  let display

  console.log('subjectCards.js: ' + gradeSelection)

  display = (
    <Route
      path="/subjects/:subjectName"
      render={props => <Subject {...props} gradeSelection={gradeSelection} />}
    />
  )

  return display
}

You can see an example of this over at https://mo9jook5y.codesandbox.io/subjects/math

You can play around with the example here: https://codesandbox.io/s/mo9jook5y

like image 145
Dennis Martinez Avatar answered Oct 10 '22 10:10

Dennis Martinez