reactjsreact-reduxreact-hooksreact-typescript

On route change data disappears, how to persist data


In my application, I am listing pokemon from backend. After the pokemons listed in the pokemon-list component, on click on any of the pokemon I am trying to show the details of the same.

But the whole data disappears suddenly on click of view details button. I am unable to understand the correct approach to implement the Redux store. What is the proper way to implement the same?

I require:

  1. on page load, all pokemons need to listed in the list component
  2. on click of view details like to show the individual pokemon
  3. when click on view-list again, need to show the pokemons as like page load, without any backend call.

I am new to Redux and React.

Live Demo


Solution

  • I forge to pass the reset of the properties with reducer. when the view details requested, list of user missed, since i did not pass the previous states. it works for me:

    const pokemonReducer = (state = initialState, action: PokemonDispatchTypes) => {
      switch (action.type) {
        case POKEMON_FAIL:
          return { loading: false };
        case POKEMON_LOADING:
          return { loading: true };
        case POKERMON_SUCCESS:
          return { loading: false, pokemons: action.payload };
        case POKEMON_DETAILS:
          return { ...state, loading: false, showPokemon: action.payload };//mistake is here
        default:
          break;
      }
      return state;
    };