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

Running useEffect in react 18: #11

Open
fmanimashaun opened this issue Oct 21, 2023 · 2 comments
Open

Running useEffect in react 18: #11

fmanimashaun opened this issue Oct 21, 2023 · 2 comments

Comments

@fmanimashaun
Copy link

I noticed the code below will not work in React 18 as the component renders twice during the initial rendering. in strict mode

 .addCase(fetchPosts.fulfilled, (state, action) => {
               state.status = 'succeeded'
               // Adding date and reactions
               let min = 1;
               const loadedPosts = action.payload.map(post => {
                   post.date = sub(new Date(), { minutes: min++ }).toISOString();
                   post.reactions = {
                       thumbsUp: 0,
                       wow: 0,
                       heart: 0,
                       rocket: 0,
                       coffee: 0
                   }
                   return post;
               });

               // Add any fetched posts to the array
               state.posts = state.posts.concat(loadedPosts)
           })
@Lam-Big-Water
Copy link

// changed
state.posts = loadedPosts

@nathanlao
Copy link

In React 18 with strict mode, after component mounts, React immediately simulates unmounting and remounting the component, update react-redux to latest version (compatible with React 18) should solve the problem.

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

3 participants