React: Passing In Properties
I am trying to pass in store as a property to AddTodo but I am getting the error: Cannot read property 'todos' of undefined import React from 'react'; import ReactDOM from 'react-d
Solution 1:
Functional components work differently, they don't have a context of this
, but their props are passed through the arguments of the function
To make it work, just change your function call of addToDos, like this:
constAddTodo = (props) => {
let input;
console.log(props.todos);
return (/* and the rest of your code...*/) ;
};
For the rest, as Arun Ghosh is saying, you should revisit your subscribe pattern, for example like this
store.subscribe(() => {
ReactDOM.render(
<AddTodotodos={store.getState()}
/>,
document.getElementById('root'));
});
Solution 2:
You should pass the store object and subscribe for changes
store.subscribe(() => {
// In our case the state will be updated when todos are setconsole.log(store.getState().todos);
});
Post a Comment for "React: Passing In Properties"