How exactly re-rendering of the components happen if there is a new state object returned from the redux reducer..?
Are there any optimisation techniques used by react to minimalize the rendering of components ..?
When you connect a component to the redux store, and you map the redux state to props, if a state changes into a new state, the component re-renders. Just like when you pass a state to a child component and that state changes.
Related
Say my entire react app listens to a single store. That store is an MobX observable. mobx.observable(someVeryLargeObjectContainingAllTheStates) So the design is like Redux's single store.
Does the whole react app rerender everytime a single property change in that store? Meaning render() is called recursively on ALL components?
For my application, I want to create a component who's sole purpose is to update when there is a change to the Redux store.
I have achieved this with a componentDidMount() that checks if the component's state matches the store and sets the state if not.
My solution works, and is suitable enough for the situation, but it seems like a lot of code for a simple function.
How can one achieve the same outcome, a component that updates on Redux store change, using a functional component or a more simple stateful component?
Update: Thanks to the comments below, I have a new working the passes state information as props.
React components renders due to changes in props and state, therefore when you connect your component to Redux store
and supply mapStateToProps, when redux store update the mentioned props that you will return from that function, your component will re-render. You can detect changes via componentWillReciveProps life cycle method.
I've found mobx observable component only rerender themselves and not their children. How is this achieved?
From the MobX documentation:
MobX reacts to any an existing observable property that is read during the execution of a tracked function.
By using #observer over a component MobX will track and react to changes occurred to observables defined inside the render function of this component.
Each child component should be wrapped with #observer if you wish it to react to changes.
Edited:
By using observer over a component, MobX will override the shouldComponentUpdate by telling the component to update only when necessary (observables change or shallow props change).
From MobX #observer documentation:
observer also prevents re-renderings when the props of the component have only shallowly changed, which makes a lot of sense if the data passed into the component is reactive. This behavior is similar to React PureRender mixin, except that state changes are still always processed. If a component provides its own shouldComponentUpdate, that one takes precedence.See for an explanation this github issue
I've read this thread. What is the difference between state and props in React?
It says that props are different from states and ideally props should not be change in its component and only should be changed by its parent component.
However, mapStateToProps function in react-redux maps states in Redux to props of a React components, which is basically changing props of a React component when Redux state is changed by an Redux action.
It does not make sense to me. It seems that it should have been mapStateToStates instead and maps Redux states to states of a React component.
Am I missing something?
It says that props are different from states and ideally props should
not be change in its component and only should be changed by its
parent component.
State here refers to the internal state of the component, which the component can change internally via .setState().
However, mapStateToProps function in react-redux maps states in Redux
to props of a React components, which is basically changing props of a
React component when Redux state is changed by an Redux action.
The state here refers to the redux store, an external state. react-redux's connect method creates a HOC - higher order component (a component which is aware of the store's state changes). The HOC wraps the dumb react component, which is not aware of the store. Using mapStateToProps the HOC maps the data from the external state, and inject it to the react component via the props.
State in redux store -> mapStateToProps in HOC -> props passed to the dumb component
So the HOC is the parent, and the dumb component is the child. The parent injects new props to the child component, and the 1st assertion "props should not be change in its component and only should be changed by its parent component" is not violated.
Notes:
More info about higher order component can be found in Dan Abramov's article about Presentational and Container Components.
To understand how react-redux connect works - in the online free course, Getting Started with Redux, Dan Abramov shows how to build connect from scratch (lessons 22-29)
state here in mapStateToProps means state in ReduxStore.
It maps Redux state to ReactComponent props. It's not necessarily to map props to states, e.g. Pure Component
mapStateToProps pass state as a props to component using react-redux connect () function. Consider connect() function as wrapper component, which passes props to children.
mapStateToProps
connect (state) -----------------------> component(props)
In react-redux library, there is almost only one API, connect(), and its only purpose is to bind "redux-things" to react's props. That's fine, but as far as I could see, the application state is handled with redux's state. I find no use of react's state anymore, since, a component could be rendered using props alone and mutate its state (via redux's state) using actions.
Am I correct ?