Essentially, I am trying to persist a user across my next.js app after they're logged in i.e. upon any browser refresh it persists the data/state.
A way to do this when using redux, is to use redux-persist!
These the are the versions:
"next": "^9.0.5",
"redux-persist": "^6.0.0",
"redux": "^4.0.4",
This is my pages/index.js file.
import { connect } from 'react-redux'
import Head from '../components/head'
import HomeLayout from '../components/Home/Home.jsx'
import 'semantic-ui-css/semantic.min.css'
import '../styles/styles.scss'
import {bindActionCreators} from 'redux'
import { logInUser } from '../store/index'
class Home extends React.Component {
static getInitialProps ({ isLoggedIn }) {
return {isLoggedIn}
}
componentDidMount() {
logInUser()
}
render() {
const { isLoggedIn } = this.props
console.log("isLoggedIn ", isLoggedIn)
return (
<div>
<Head title = 'Home' />
<HomeLayout isLoggedIn = { isLoggedIn }/>
</div>
)
}
}
const mapDispatchToProps = { logInUser }
export default connect(state =>
state,
mapDispatchToProps,
)(Home)
And this is my store
import { createStore, applyMiddleware } from 'redux';
import { composeWithDevTools } from 'redux-devtools-extension';
import { persistReducer } from 'redux-persist';
import storage from 'redux-persist/lib/storage' // defaults to localStorage for web
import { createLogger } from 'redux-logger'
import thunkMiddleware from 'redux-thunk';
/* initial state */
var startState = { isLoggedIn: false }
/* action types */
export const actionTypes = {
IS_LOGGED_IN: 'IS_LOGGED_IN'
}
/* reducer(s) */
export const reducer = (state = initialState, action) => {
switch (action.type) {
case 'IS_LOGGED_IN':
return Object.assign({}, state, {
isLoggedIn: action.isLoggedIn
})
default:
return state
}
};
/* actions */
export const logInUser = () => {
return { type: actionTypes.IS_LOGGED_IN, isLoggedIn: true, }
}
const persistConfig = {
key: 'root',
storage,
// whitelist: ['exampleData'] // place to select which state you want to persist
}
const persistedReducer = persistReducer(persistConfig, reducer)
// create a store
export const initializeStore = (initialState = startState) => {
return createStore(
persistedReducer,
initialState,
composeWithDevTools(applyMiddleware(
thunkMiddleware,
createLogger({ collapsed: true })
))
)
}
Any help would be appreciated!
UPDATE:
Also wanted to add my logger output from redux:
Strangely the action is not getting fired? The payload seems to still be false?

