Skip to content

Commit

Permalink
Improved everything
Browse files Browse the repository at this point in the history
  • Loading branch information
kirill-konshin committed Feb 17, 2017
1 parent f0d3e85 commit 0ebca3d
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 91 deletions.
2 changes: 1 addition & 1 deletion next.js/components/Layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from "react";
import {connect} from "react-redux";
import Head from "next/head";

export default (({children, reduxStatus, title = 'Wat?'}) => (
export default connect(state => state)(({children, reduxStatus, title = 'Wat?'}) => (
<div className="container">
<Head>
<title>{title} | Next</title>
Expand Down
76 changes: 0 additions & 76 deletions next.js/lib/connectToProvider.js

This file was deleted.

72 changes: 72 additions & 0 deletions next.js/lib/provide.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import React from "react";
import {Provider} from "react-redux";

let memoizedStore;

const initStore = (makeStore, isServer, initialState) => {

// Always make a new store if server
if (isServer && typeof window === 'undefined') {
return makeStore(initialState);
}

// Memoize store if client
if (!memoizedStore) {
memoizedStore = makeStore(initialState);
}

return memoizedStore

};

export default (createStore) => {

return (Cmp) => {

const WrappedCmp = ({initialState, isServer, store, ...props}) => {

if (!store || !store.dispatch) {
store = initStore(createStore, isServer, initialState);
//console.log('4. WrappedCmp.render created new store with initial state', initialState);
} else {
//console.log('4. WrappedCmp.render picked up existing store');
}

return (
<Provider store={store}>
<Cmp {...props}/>
</Provider>
);

};

/**
* @param dialog
* @return {{store, isServer: boolean, initialState}}
*/
WrappedCmp.getInitialProps = async(dialog) => {

const isServer = !!dialog.req;
const store = initStore(createStore, isServer);

// console.log('1. WrappedCmp.getInitialProps wrapper creates the store');

if (Cmp.getInitialProps) {
await Cmp.getInitialProps.call(Cmp, {...dialog, isServer, store});
}

// console.log('3. WrappedCmp.getInitialProps has store state', store.getState());

return {
store,
isServer,
initialState: store.getState()
};

};

return WrappedCmp;

};

};
10 changes: 6 additions & 4 deletions next.js/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import React from "react";
import {connect} from "react-redux";
import Link from "next/link";
import {makeStore} from "../lib/store";
import connectToProvider from "../lib/connectToProvider";
import provide from "../lib/provide";
import Layout from "../components/Layout";

class Page extends React.Component {

static getInitialProps({store, isServer, pathname, query}) {

console.log('2. Page.getInitialProps uses the store to dispatch things, pathname', pathname, 'query', query);
// console.log('2. Page.getInitialProps uses the store to dispatch things, pathname', pathname, 'query', query);

// If it's a server, then all async actions must be done before return or return a promise
if (isServer) {
Expand All @@ -28,7 +29,7 @@ class Page extends React.Component {
}

render() {
console.log('5. Page.render');
// console.log('5. Page.render');
return (
<Layout title="Index">
<h2>Index</h2>
Expand All @@ -41,6 +42,7 @@ class Page extends React.Component {

}

Page = connectToProvider(makeStore, state => state)(Page);
Page = connect(state => state)(Page);
Page = provide(makeStore)(Page); // note that provide should be last

export default Page;
20 changes: 10 additions & 10 deletions next.js/pages/other.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import Link from "next/link";
import React from "react";
import Layout from "../components/Layout";
import {makeStore} from "../lib/store";
import provide from "../lib/provide";

export default () => {
return (
<Layout title="Other">
<h2>Other</h2>
<nav>
<Link href="/"><a>Navigate to index</a></Link>
</nav>
</Layout>
)
};
export default provide(makeStore)(() => (
<Layout title="Other">
<h2>Other</h2>
<nav>
<Link href="/"><a>Navigate to index</a></Link>
</nav>
</Layout>
));

0 comments on commit 0ebca3d

Please sign in to comment.