question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

React doesn't provide an API witch is doing some side affects from top to bottom.

See original GitHub issue

Do you want to request a feature or report a bug? feature

What is the current behavior? The current version of react requires lifecycles constructor and render must be pure functions and don’t do any side affect. We can only do side affects in componentDidMount.

But it’s unfriendly with some frameworks which have got to memorize the order from ancestor component instance to child component instance.

In other words, it’s like topological sort. Even you can reverse array to achieve this goal, But it’s impossible to distinguish what’s the end of this array because react doesn’t provide an API to let framework knows the rendering is done.

componentDidMount are always called from child instance to ancestor instance, so react is missing an approach which is doing side affects from top to bottom.

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem. Your bug will get fixed much faster if we can run your code and it doesn’t have dependencies other than React. Paste the link to your JSFiddle (https://jsfiddle.net/Luktwrdm/) or CodeSandbox (https://codesandbox.io/s/new) example below: https://codesandbox.io/s/wk57nm7rm5 /src/rako-react/prop.js line: 78

What is the expected behavior?

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React? It can work properly in sync rendering if you do side affect in constructor, but it would cause unexpected result(memory leak) in async rendering because a node may be rendered multiple times.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8

github_iconTop GitHub Comments

3reactions
tonaicommented, Oct 31, 2018

Thanks @malerba118 for your example.

In fact it would be great if getDerivedStateFromProps could have access to the next and prev values for props and state (as previous API componentWillUpdate did).

To me getDerivedStateFromProps seems more like a regression of the API since it enforce you to do some trick, like copying props into the state, o achieve what you want…

1reaction
rabbitooopscommented, Nov 1, 2018

Seems it’s impossible to provide such API because its usage scenario is limited, but I think a hook React.didRender which is making other frameworks to perceive a rendering is done is necessary.

In addition, React.beforeRender for doing some initialization. They can be simplify to one API like useEffect.

React.subscribeRender(() => {
  // beforeRender
  return () => {
    // didRender
  }
})

I think React is not flexible enough because it doesn’t provide enough hooks APIs for more lower-level developers.

I am tired and not going to develop for react, close it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using the Effect Hook - React
The Effect Hook lets you perform side effects in function components: ... Sometimes, we want to run some additional code after React has...
Read more >
Context - React
Context provides a way to pass data through the component tree without having to pass props down manually at every level. In a...
Read more >
React Top-Level API
React is the entry point to the React library. If you load React from a <script> tag, these top-level APIs are available on...
Read more >
Suspense for Data Fetching (Experimental) - React
Suspense lets your components “wait” for something before they can render. In this example, two components wait for an asynchronous API call to...
Read more >
Strict Mode - React
StrictMode is a tool for highlighting potential problems in an application. Like Fragment , StrictMode does not render any visible UI. It activates...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found