Реакция - Как я могу сделать состояния сохраняются после обновления страницы sh (используя Redux-persist) - PullRequest
0 голосов
/ 18 марта 2020

Я создаю одностраничное приложение, используя React. js. Все штаты работают хорошо. Я нашел решение, использующее библиотеку 'redux-persist' с индексом. js для хранения состояний после обновления страницы sh, и я попытался это сделать.

store. js

import { applyMiddleware, combineReducers, createStore } from 'redux'
import { ADD_TO_CART, GET_COURSE_LIST, USUARIO_LOGIN } from './action'
import { composeWithDevTools } from 'redux-devtools-extension'
import { persistStore, persistReducer } from 'redux-persist'
import thunk from 'redux-thunk'



const initialCart = {
    cart:[]
}

const initialCourses ={
    courses:[]
}

const initialUser ={
    usuario:{}
}

const cartReducer = ( state = initialCart,action) => {
    console.log(action)

    if(action.type===ADD_TO_CART)
    {

        if(state.cart.find(c=>c===action.id)) 
        {
            return state
        }

        return{
            ...state,
            cart: state.cart.concat(action.id),            
        }

    }
    return state

}

const coursesReducer = (state=initialCourses, action) =>{
    console.log(action)
    if(action.type === GET_COURSE_LIST){
        return {
            ...state,
            courses: action.courses
        }
    }
    return state
}

const userReducer = (state=initialUser, action)=>{
    console.log(action)
    if(action.type === USER_LOGIN){
        return {
            ...state,
            user: action.user
        }
    }
    return state

}

export default createStore(combineReducers({cartReducer, coursesReducer, userReducer}), composeWithDevTools(applyMiddleware(thunk)))

App.jsx

import React from 'react';
import '../App.css';
import AppRoutes from './AppRoutes';
import  { Provider }  from "react-redux"
import store from '../redux/store'
import { getCoursesList } from '../redux/actionCreators'
import {createStore} from 'redux'



store.dispatch(getCoursesList())

const App = () => (
    <Provider store={store}>

        <AppRoutes />

    </Provider>
  )

export default App;

index. js

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './components/App.jsx';
import * as serviceWorker from './serviceWorker';
import {Provider} from 'react-redux';
import {applyMiddleware, combineReducers, createStore} from "redux";
import {persistReducer, persistStore} from 'redux-persist';
import {PersistGate} from 'redux-persist/integration/react';
import storage from 'redux-persist/lib/storage';
import thunk from "redux-thunk";

const persistConfig = {
    key: 'root',
    storage,
};

const persistedReducer = persistReducer(persistConfig, allReducers);
let store = createStore(persistedReducer, applyMiddleware(thunk));
let persistor = persistStore(store);


ReactDOM.render(<Provider store={store}>
                    <PersistGate loading={null} persistor={persistor}>
                        <App />
                    </PersistGate>
                </Provider>, document.getElementById('root'));

My проблема в индексе. js Я не могу импортировать AllReducers, потому что там, где я храню все редукторы, хранится. js, и это работает с 'mixedReducers', поэтому мои состояния не могут сохраняться после обновления страницы sh

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...