From 12791c44293e6788452124f4fba67611b62d537f Mon Sep 17 00:00:00 2001 From: handtrix Date: Fri, 27 Sep 2019 11:22:54 +0200 Subject: [PATCH] Simplify with-redux example ## Changes - [x] Remove _app.js usage - [x] Migrate withRedux HOC to functional component - [x] Add correct display name - [x] Remove abstractions/boilerplate from example - [x] Add useInterval HOC from Dan --- examples/with-redux/README.md | 9 +-- examples/with-redux/components/clock.js | 25 ++++++- examples/with-redux/components/counter.js | 33 +++++----- examples/with-redux/components/examples.js | 20 ------ examples/with-redux/lib/redux.js | 72 +++++++++++++++++++++ examples/with-redux/lib/useInterval.js | 19 ++++++ examples/with-redux/lib/with-redux-store.js | 50 -------------- examples/with-redux/package.json | 4 +- examples/with-redux/pages/_app.js | 17 ----- examples/with-redux/pages/index.js | 62 ++++++++++-------- examples/with-redux/store.js | 68 +++++++------------ 11 files changed, 190 insertions(+), 189 deletions(-) delete mode 100644 examples/with-redux/components/examples.js create mode 100644 examples/with-redux/lib/redux.js create mode 100644 examples/with-redux/lib/useInterval.js delete mode 100644 examples/with-redux/lib/with-redux-store.js delete mode 100644 examples/with-redux/pages/_app.js diff --git a/examples/with-redux/README.md b/examples/with-redux/README.md index 68d6cf5971602..df7ceff24d998 100644 --- a/examples/with-redux/README.md +++ b/examples/with-redux/README.md @@ -45,13 +45,10 @@ Usually splitting your app state into `pages` feels natural but sometimes you'll In the first example we are going to display a digital clock that updates every second. The first render is happening in the server and then the browser will take over. To illustrate this, the server rendered clock will have a different background color (black) than the client one (grey). -The Redux `Provider` is implemented in `pages/_app.js`. Since the `MyApp` component is wrapped in `withReduxStore` the redux store will be automatically initialized and provided to `MyApp`, which in turn passes it off to `react-redux`'s `Provider` component. +The Redux `Provider` is implemented in `lib/redux.js`. Since the `IndexPage` component is wrapped in `withRedux` the redux context will be automatically initialized and provided to `IndexPage`. -`index.js` have access to the redux store using `connect` from `react-redux`. -`counter.js` and `examples.js` have access to the redux store using `useSelector` and `useDispatch` from `react-redux@^7.1.0` +All components have access to the redux store using `useSelector`, `useDispatch` or `connect` from `react-redux`. On the server side every request initializes a new store, because otherwise different user data can be mixed up. On the client side the same store is used, even between page changes. -The example under `components/counter.js`, shows a simple incremental counter implementing a common Redux pattern of mapping state to props. Again, the first render is happening in the server and instead of starting the count at 0, it will dispatch an action in redux that starts the count at 1. This continues to highlight how each navigation triggers a server render first and then a client render when switching pages on the client side - -For simplicity and readability, Reducers, Actions, and Store creators are all in the same file: `store.js` +The example under `components/counter.js`, shows a simple incremental counter implementing a common Redux pattern of mapping state to props. Again, the first render is happening in the server and instead of starting the count at 0, it will dispatch an action in redux that starts the count at 1. This continues to highlight how each navigation triggers a server render first and then a client render when switching pages on the client side. diff --git a/examples/with-redux/components/clock.js b/examples/with-redux/components/clock.js index 4e71fc3a92f96..7d9de8ad1a8a2 100644 --- a/examples/with-redux/components/clock.js +++ b/examples/with-redux/components/clock.js @@ -1,7 +1,26 @@ -export default ({ lastUpdate, light }) => { +import React from 'react' +import { useSelector, shallowEqual } from 'react-redux' + +const useClock = () => { + return useSelector( + state => ({ + lastUpdate: state.lastUpdate, + light: state.light + }), + shallowEqual + ) +} + +const formatTime = time => { + // cut off except hh:mm:ss + return new Date(time).toJSON().slice(11, 19) +} + +const Clock = () => { + const { lastUpdate, light } = useClock() return (
- {format(new Date(lastUpdate))} + {formatTime(lastUpdate)}