Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

330
Visualizações
Save and retrieve states with redux-persist - React Native

I'm new to react-native and I'm trying for the first time to save the states of my app.

In order to achieve this I've been suggested to use redux-persist. I expected to find more examples and topics about it in the web and I feel that my idea about how redux-persist works is not that limpid.

I've followed the short guide in the github page but once I close and re-open my app I can't see saved states values but I see the default, initial states values.

This is my app.js file:

import React, { Component } from 'react';
import { AsyncStorage } from 'react-native'
import { Provider } from 'react-redux';
import { createStore, applyMiddleware, compose } from 'redux';
import { persistStore, autoRehydrate } from 'redux-persist';
import ReduxThunk from 'redux-thunk';
import reducers from './reducers';
import Router from './Router';

class App extends Component {
  render() {
    const store = compose(applyMiddleware(ReduxThunk), autoRehydrate())(createStore)(reducers); 
    persistStore(store, {storage: AsyncStorage}, () => {
        console.log('restored');
    })
    return (
      <Provider store={store}>
        <Router />
      </Provider>
    );
  }
}

export default App;

Do I need something else?

I'd also like to know how I can console.log all the states values inside the redux-persistor storage.

Thanks in advance

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You have put persistStore into render method. Persistence call is a side-effect, that should never happen within render.

Move it to componentWillMount, as redux-persist documentation says to you

export default class AppProvider extends Component {

  constructor() {
    super()
    this.state = { rehydrated: false }
  }

  componentWillMount(){
    persistStore(store, {}, () => {
      this.setState({ rehydrated: true })
    })
  }

  render() {
    if(!this.state.rehydrated){
      return <div>Loading...</div>
    }
    return (
      <Provider store={store}>
         {() => <App />}
      </Provider>
    );
  }
}

You need to defer initialization before store will be rendered. This is what this code snippet does

over 4 years ago · Santiago Trujillo Relatório

0

@just-boris's answer may or may not solve this for you, but regardless you should definitely move the call to persistStore out of the render function for exactly the reasons they mention.


It's hard to tell what's going wrong when there aren't any errors, but I suspect that handling the persist/REHYDRATE action in one of your reducers will help you find the problem.

case 'persist/REHYDRATE': {
  console.log(action.payload) // persist's action uses "payload" not "data"

  return state // NOP, just wanted to log the payload
}

You can also use your browser's inspector tool to inspect local storage, which will give you an idea of what is being stored inside redux-persist.

enter image description here

over 4 years ago · Santiago Trujillo Relatório

0

You Can Use

store.getState().whatEver;
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda