Is it possible to recursive render React component connected to Redux store?
Example (in my case there is no chance to do infinite component rendering loop):
class Container extends Component {
render (){
return (
<div>
{this.props.data}
{this.props.dataKey ? <Container dataKey={'123'} /> : null}
</div>
}
}
const mapStateToProps = (state, props) => {
return {
data: getDataFromStore(state, props.dataKey}
}
}
export default connect(mapStateToProps)(Container)
I saw that i can render component in component, but nested component have not connection to the store and hance i do not have required this.props.data
.
Is any chance to get nested component connected to the store?
It deduces the changes of your data by running the reducer function you provide, and returns the next state that corresponds to every action dispatched. React Redux then optimizes component rendering and makes sure that each component re-renders only when the data it needs change.
There are two stages in React rendering: Call the render() function to render to the virtual DOM. Compare the real DOM with the virtual DOM, and update it accordingly.
A Practical Use for Recursion in React Follow along to understand the ins and outs of a working React Tree component powered by recursion + React rendering. Giving our component nested data in the following shape, we'll now have a functioning, modular, and recursive component ready for use!
Overview The connect() function connects a React component to a Redux store. It provides its connected component with the pieces of the data it needs from the store, and the functions it can use to dispatch actions to the store.
Try to render already connected Container
:
class Container extends Component {
render (){
return (
<div>
{this.props.data}
{this.props.dataKey ? <ConnectedContainer dataKey={'123'} /> : null}
</div>
);
}
}
const mapStateToProps = (state, props) => {
return {
data: getDataFromStore(state, props.dataKey}
}
}
const ConnectedContainer = connect(mapStateToProps)(Container);
export default ConnectedContainer;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With