Serhan Özcan
Serhan Özcan

Reputation: 51

Redux not update nasted state

I have an array and i'm dispatching LIKE_POST action from child component. Redux state updated but view not updating. I'm using Flatlist. And using extradata. (Working ADD_POST and DELETE_POST actions)

action.payload returning liked post id,

In reducer:

    const initialState = {
        feedData: [],
        loading: false,
    };
...
//FETCH FEED
...
    case LIKE_POST:
                return {
                    ...state,
                    feedData: [
                        ...state.feedData.map((post) => {
                            if (post.id === action.payload) {
                                return {
                                    ...post,
                                    like: {
                                        count: 1,
                                        isLiked: true,
                                    },
                                };
                            }
                            return post;
                        }),
                    ],
                };

Logger is returning like:1 and isLiked:true but view not updated.

Upvotes: 1

Views: 79

Answers (1)

Serhan Özcan
Serhan Özcan

Reputation: 51

I solved the problem.

I'm using useState hook in the component for feedData. When i remove useState and directly using stored data. It's worked.

const feedData = useSelector(state=>state.feed.feedData)
const [data, setData] = useState(feedData) => THIS IS THE MISTAKE

Upvotes: 1

Related Questions