I can't seem to figure out how to use redux. I want to save an array of lists to the local storage and add a new list each time but I'm getting undefined for the lists variable. ............................................................................ This is my store
store.js
import { createStore, compose, applyMiddleware, combineReducers } from 'redux';
import thunk from 'redux-thunk';
import { listsReducer } from "./reducers/ListsReducer";
const initialState = {
lists: localStorage.getItem('lists')
?JSON.parse(localStorage.getItem('lists'))
:[]
}
const reducer = combineReducers({
lists: listsReducer
});
const composeEnhancer = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const store = createStore(
reducer,
initialState,
composeEnhancer(applyMiddleware(thunk))
);
export default store;
........................................................................... My reducer
ListsReducer.js
import { LISTS_ADD_LIST } from "../constants/ListsConstants";
export const listsReducer = (state = {lists:[]}, action) =>{
switch(action.type){
case LISTS_ADD_LIST:
const list = action.payload;
return {
...state,
lists: [...state.lists, list]
};
default:
return state;
}
}
........................................................................... ListsConstants.js
export const LISTS_ADD_LIST = 'LISTS_ADD_LIST';
........................................................................... index.js
import React from 'react';
import ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import App from './App';
import store from './store';
ReactDOM.render(
<Provider store={store}>
<React.StrictMode>
<App />
</React.StrictMode>
</Provider>,
document.getElementById('root')
);
................................................................................ App.js
import {BrowserRouter} from 'react-router-dom';
import { useDispatch } from 'react-redux';
import { addNewList } from "../actions/ListsActions";
function App() {
const dispatch = useDispatch();
const addList = ()=>{
var date_time = new Date().toLocaleString();
var title = 'my list '+date_time;
var list = {
_id:date_time,
title:title,
items:[]
};
dispatch(addNewList(list));
}
return(
<div>
<button onClick={addList}> </button>
</div>
);
}
................................................................................ ListsActions.js
import { LISTS_ADD_LIST } from "../constants/ListsConstants";
export const addNewList = (list) => async(dispatch, getState) => {
dispatch({
type: LISTS_ADD_LIST,
payload: list
});
localStorage.setItem('lists', JSON.stringify(getState().lists));
};