farm command
farm command

Reputation: 693

fire componentDidMount without having render method

I use React and Redux in my app. I'm trying to make my container clear from any JSX code and even render method. I am trying to use my container just to fetch data and then at the export section I use connect and push those data to my other component that I have my JSX codes.

The problem is, when I did this I noted that the redux-action I have in the componentdidMount to fetch the data wasn't run and I noted that is because my componentDidMount didn't get called. So what shall I do?

Upvotes: 0

Views: 63

Answers (1)

SirFunkButter
SirFunkButter

Reputation: 66

Your biggest issue is that componentDidMount is only called after render succeeds. This means that you shouldn't even expect it to get called if you have no render. Part of the idea with React lifecycle methods is that they revolve around rendering and state but if you have no render, changes to state and detecting re-renders as componentDidMount is meant to do can't happen. componentDidMount implies that there is something to mount which is the job of the render function.

If you look at this diagram you can see there is no work around to call a lifecycle function dependent on render without it. render is the only thing that can trigger changes to the DOM to which componentDidMount listens.

Perhaps consider doing something like explained in the componentDidMount section here where your render returns the data that componentDidMount collects and then that state could be used in your other components as props.

Read more about React lifecycle methods here

Upvotes: 1

Related Questions