Skip to content

Instantly share code, notes, and snippets.

View oliverlin's full-sized avatar

Oliver² oliverlin

  • infocast
  • Taiwan
View GitHub Profile
// _app.js
function MyApp({ Component, pageProps }) {
const preloadedState = pageProps
// Initial server-side and client side initial render
const [store] = useState(() => {
return initStore(preloadedState)
})
// Handle server-side props passed in when switching pages
useEffect(() => {
// _app.js
function MyApp({ Component, pageProps }) {
const preloadedState = pageProps
// Initial server-side and client side initial render
const [store] = useState(() => {
return initStore(preloadedState)
})
return (
<Provider store={store}>
// pages/jobs.js
import Jobs from '../components/Jobs'
export default Jobs
// components/Jobs.js
import { addJob, removeJob } from '../data/jobs'
const Jobs = () => {
return <></>
}
const mapStateToProps = (state) => {
// createStore.js
const reducer = combine(reducerRegistry.getReducers())
const store = createStore(reducer)
reducerRegistry.setChangeListener(reducers => {
store.replaceReducer(combine(reducers))
})
// data/jobs.js
export default function reducer(state = initialState, action = {}) {
@oliverlin
oliverlin / code-splitting-1.js
Last active May 7, 2021 09:49
code-splitting redux reducers in next.js 1
// createStore.js
import jobs from 'reducers/jobs'
import books from 'reducers/books'
const reducers = {
jobs,
books
}
const store = createStore(combineReducers(reducers))