Othman
Othman

Reputation: 72

react : changes on context does not re-render my components

Hello everyone,

Hoping I have made myself clear and gave enough informations.

edit : sorry to everyone, I think I have updated my sandbox, first time using it and got confused by my local storage I guess.

Upvotes: 1

Views: 527

Answers (1)

Pavlos Karalis
Pavlos Karalis

Reputation: 2966

Your sandbox code is showing empty, though the issue may be because you aren't storing your context value within your app component's state. If you change a static value, a re-render won't be triggered.

See this working example: https://codesandbox.io/s/eager-fermat-6ji6y?file=/src/App.js

Edit: Your reducer was mutating state directly, change to below and it works

export const planetReducer = (planetsTab, action) => {
  switch (action.type) {
    case "switchPlanetToggle":
      return [
        ...planetsTab.slice(0, action.index),
        {
          ...planetsTab[action.index],
          planetToggle: !planetsTab[action.index].planetToggle
        },
        ...planetsTab.slice(action.index + 1)
      ];
    default:
      return planetsTab;
  }
};

https://codesandbox.io/s/twilight-sea-lwwep?file=/src/PlanetsContext.js

Upvotes: 1

Related Questions