Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: Sometimes useEffect() callback is called before render #19100

Closed
ildar-icoosoft opened this issue Jun 8, 2020 · 3 comments
Closed

Bug: Sometimes useEffect() callback is called before render #19100

ildar-icoosoft opened this issue Jun 8, 2020 · 3 comments
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug

Comments

@ildar-icoosoft
Copy link

From documentation: "What does useEffect do? By using this Hook, you tell React that your component needs to do something after render".

Here is example of my code

Please take a look at Square component. If you open this example you will see the animation of Square component.

It happens because of CSS. First we set
transform: translate(0px, 0px)
then we change it to:

transform: `translate(420px, 420px)`,
transition: `transform 5000ms`,

I use "useEffect" hook to change styles. useEffect is called after first render and the animation is started.

But in some cases this does not happen. Please take a look at "App" component. It renders Square and DropAreaWithDndContext components. DropAreaWithDndContext has "useDrop" hook. And if you use App component instead of Square component then the red square will not move.

Please change the last line from:
ReactDOM.render(<Square />, document.querySelector("#root"));
to
ReactDOM.render(<App />, document.querySelector("#root"));

You will see that the Square does not move. It will move only if you change:

useEffect(() => {
    setFirstRender(false);
}, []);

to this:

useEffect(() => {
    setTimeout(() => {
        setFirstRender(false);
    }, 0);
}, []);

So, in this example useEffect() callback is called before the first render.

React version: 16.8.6

Link to code example: https://codesandbox.io/s/reactjs-playground-4q9q8?file=/src/index.js

@ildar-icoosoft ildar-icoosoft added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Jun 8, 2020
@vkurchatkin
Copy link

This has nothing to do with React, that's just how CSS transitions work

@ildar-icoosoft
Copy link
Author

ildar-icoosoft commented Jun 8, 2020

This has nothing to do with React, that's just how CSS transitions work

Why useDrop hook affected to this? (without useDrop() the animation works without setTimeout())

@vkurchatkin
Copy link

It's hard to tell exactly, but various work can affect the way a browser batches painting that can essentially disable transitions like this. You can read more about it here: reactjs/react-transition-group#159

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug
Projects
None yet
Development

No branches or pull requests

2 participants