1

So I'm quite new to react and node, and found it easiest for me to use node (express) server and then just linking to react using CDN. Until now everything has worked fine this way, but I would like to use something like componentDidMount() for rendering a list of books. Googled a little and found out that componentDidMount() is not fired when rendering via the server. So I was wondering if there is an alternative way, that can do the same thing. Tried with componentWillMount() as well, but no change.

Thanks for any advice

5
  • What's your case? If it's an operation that you cannot use a constructor for, it's likely unsuitable for SSR. Commented Nov 22, 2018 at 8:11
  • was thinking to fetch data, and then displaying it on page render Commented Nov 22, 2018 at 8:25
  • Do you intend to render fetched data on server sdie? Commented Nov 22, 2018 at 8:31
  • The plan was to render on client side, i have a method in React class that sends a fetch(), method: Get request to the server that executes a query to the database and sends the response back to the client Commented Nov 22, 2018 at 8:39
  • Currently SSR is synchronous. A request likely shouldn't be coupled with a component then. Fetch it separately, pass hydration data to client side. Things are expected to change in 17. Commented Nov 22, 2018 at 8:47

1 Answer 1

2

It is not possible. In an isomorphic react app, componentDidMount will only be called after the hydration is done in the client.

So, if you want to render something based on props or state, you can use the class constructor or the legacy componentWillMount as these methods will be called during render done in node.

Hope this helps!

Sign up to request clarification or add additional context in comments.

1 Comment

i'm getting some values from another api on componentDidMount method but i want to get values on server side and send to the client. So how do i use constructor class to achieve this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.