Search⌘ K
AI Features

Possible Gotcha

Explore the potential pitfalls of assigning state directly from the Redux store in React applications. Learn why using Redux as the single source of truth for state management is crucial, and how to properly feed Redux state as props to components while avoiding React's local setState method.

We'll cover the following...

In the just concluded Hello World example, a possible solution you may have come up with for grabbing the state from the STORE may look like this:

Javascript (babel-node)
class App extends Component {
state = store.getState();
render() {
return <HelloWorld tech={this.state.tech} />;
}
}

What do you think? Will this work? Just as a reminder, the following two ways are correct ways to initialise a React component’s state.

(a)

Javascript (babel-node)
class App extends Component {
constructor(props) {
super(props);
this.state = {}
}
}

(b)

Javascript (babel-node)
class App extends Component {
state = {}
}

So, back to answering the question, yes, the solution will work just fine.

store.getState() ...