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

useEffect be called on re-render in HMR will cause problems #384

Closed
brickspert opened this issue May 8, 2021 · 2 comments
Closed

useEffect be called on re-render in HMR will cause problems #384

brickspert opened this issue May 8, 2021 · 2 comments

Comments

@brickspert
Copy link

import React, { useEffect, useState } from 'react';

export default () => {
  const [count, setState] = useState(0);

  useEffect(() => {
    setState(s => s + 1);
  }, []);

  return (
    <div>
      {count}
    </div>
  )
}

Without HRM, the count Maximum will be 1.

But with HRM, the count increases with updates.

Is this the expected behavior?

Another bad example, react-use's useUpdateEffect will be called at the same time of useEffect when HRM.

import React, { useState } from 'react';
import  { useUpdateEffect } from 'react-use';

export default () => {

  useEffect(() => {
     console.log('mount')
  }, []);

 useUpdateEffect(()=>{
     console.log('updateEffect')
 }, []);

  return (
    <div>
       hello world
    </div>
  )
}
@brickspert
Copy link
Author

related facebook/react#21019

@brickspert brickspert changed the title Empty deps useEffect be called on re-render in HMR will cause problems useEffect be called on re-render in HMR will cause problems May 8, 2021
@pmmmwh
Copy link
Owner

pmmmwh commented May 8, 2021

This is expected behaviour, you can see Dan's explanation in the linked issue on why this is the case.

@pmmmwh pmmmwh closed this as completed May 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants