Skip to content

Running useEffect in react 18: #11

Open
@fmanimashaun

Description

@fmanimashaun

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)
           })

Activity

Lam-Big-Water

Lam-Big-Water commented on Dec 7, 2024

@Lam-Big-Water
// changed
state.posts = loadedPosts
nathanlao

nathanlao commented on Jan 29, 2025

@nathanlao

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @fmanimashaun@nathanlao@Lam-Big-Water

        Issue actions

          Running useEffect in react 18: · Issue #11 · gitdagray/react_redux_toolkit