Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursive rendering of React component connected to Redux store

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?

like image 614
crowmw Avatar asked Jun 14 '18 09:06

crowmw


People also ask

How does a Redux connected component know when to re render?

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.

What is the complete cycle of re rendering a component using Redux?

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.

Can React components be recursive?

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!

Which component can connect to Redux store and return an action?

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.


1 Answers

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;
like image 177
hsz Avatar answered Sep 23 '22 22:09

hsz