@@ -97,6 +97,8 @@ with deeply nested objects, a
9797 * [ removeCallback] ( #removecallback )
9898 * [ resetGlobal] ( #resetglobal )
9999 * [ setGlobal] ( #setglobal )
100+ * [ WithGlobal] ( #withglobal )
101+ * [ withInit] ( #withinit )
100102* [ Frequently Asked Questions] ( https://github.com/CharlesStover/reactn/blob/master/FAQ.md )
101103* [ Support] ( #support )
102104
@@ -123,7 +125,7 @@ attempt to render.
123125It is recommended that you initialize the global state just prior to mounting
124126with ` ReactDOM ` .
125127
126- ``` JavaScript
128+ ``` javascript
127129import React , { setGlobal } from ' reactn' ;
128130import ReactDOM from ' react-dom' ;
129131import App from ' ./App' ;
@@ -156,7 +158,7 @@ functions.
156158In order to tell TypeScript the shape of your global state when you are not using
157159a Provider, create a file at ` src/global.d.ts ` with the following contents:
158160
159- ``` JavaScript
161+ ``` javascript
160162import ' reactn' ;
161163
162164declare module ' reactn/default' {
@@ -223,7 +225,7 @@ directly into the React namespace. As a result, `Component` and `PureComponent`
223225will have access to the ` global ` and ` dispatch ` member variables and
224226` setGlobal ` method.
225227
226- ``` JavaScript
228+ ``` javascript
227229import React from ' reactn' ; // <-- reactn
228230import Card from ' ../card/card' ;
229231
@@ -273,7 +275,7 @@ By importing React and ReactN separately, the React namespace remains
273275unchanged. You can inject ReactN's global functionality into your vanilla React
274276component by using the ` @reactn ` decorator imported from the ` reactn ` package.
275277
276- ``` JavaScript
278+ ``` javascript
277279import React from ' react' ;
278280import reactn from ' reactn' ; // <-- reactn
279281import Card from ' ../card/card' ;
@@ -324,7 +326,7 @@ export default class Cards extends React.PureComponent {
324326Using [ React Hooks] ( https://reactjs.org/docs/hooks-intro.html ) , you can harness
325327` useGlobal ` to access the global state.
326328
327- ``` JavaScript
329+ ``` javascript
328330import React , { useGlobal } from ' reactn' ; // <-- reactn
329331import Card from ' ../card/card' ;
330332
@@ -354,7 +356,7 @@ export default Cards;
354356You may also use the ` useDispatch ` hook analogously to the ` useReducer ` hook by
355357providing a function to ` useDispatch ` .
356358
357- ``` JavaScript
359+ ``` javascript
358360import React , { useDispatch } from ' reactn' ; // <-- reactn
359361
360362const incrementReducer = (global , dispatch , action ) => ({
@@ -385,7 +387,7 @@ By providing a second parameter to `useDispatch` that is the key of the global
385387state, the return value of that reducer will set that property of the global
386388state. This allows you to write your reducers similar to React's ` useReducer ` .
387389
388- ``` JavaScript
390+ ``` javascript
389391import React , { useDispatch } from ' reactn' ; // <-- reactn
390392
391393const incrementReducer = (count , action ) =>
@@ -422,7 +424,7 @@ new global state will trigger the very same callback.
422424
423425The only parameter is the callback function.
424426
425- ``` JavaScript
427+ ``` javascript
426428import { addCallback , setGlobal } from ' reactn' ;
427429
428430// Every time the global state changes, this function will execute.
@@ -446,7 +448,7 @@ setGlobal({ value: 1 });
446448The return value of ` addCallback ` is a function that, when executed, removes
447449the callback.
448450
449- ``` JavaScript
451+ ``` javascript
450452import { addCallback , setGlobal } from ' reactn' ;
451453
452454const removeAlert = addCallback (global => {
@@ -479,7 +481,7 @@ when dispatching. The reducer function that you _use_ when dispatching does not
479481contain the global state or map of reducers. Those are prefixed for you
480482automatically.
481483
482- ``` JavaScript
484+ ``` javascript
483485import { addReducer , setGlobal , useDispatch , useGlobal } from ' reactn' ;
484486
485487// Initialize the global state with the value 0.
@@ -523,7 +525,7 @@ For a class component, the analogous method is
523525The ` dispatch ` parameter on a reducer allows you to write "sagas," or a single
524526reducer that dispatches other reducers.
525527
526- ``` JavaScript
528+ ``` javascript
527529// add(1)
528530addReducer (' add' , (global , dispatch , i ) => ({
529531 x: global .x + i,
@@ -555,7 +557,7 @@ should use `useDispatch` or `this.dispatch`.
555557
556558` getDispatch ` has no parameters.
557559
558- ``` JavaScript
560+ ``` javascript
559561import { getDispatch } from ' reactn' ;
560562
561563// Access this.dispatch.reducerName outside of a Component.
@@ -577,7 +579,7 @@ nothing more than return a current snapshot of the global state.
577579
578580` getGlobal ` has no parameters.
579581
580- ``` JavaScript
582+ ``` javascript
581583import { getGlobal } from ' reactn' ;
582584
583585// Access this.global.value outside of a Component.
@@ -596,7 +598,7 @@ the return value of `addCallback`.
596598
597599The only parameter is the callback function itself.
598600
599- ``` JavaScript
601+ ``` javascript
600602import { addCallback , removeCallback , setGlobal } from ' reactn' ;
601603
602604function alertCallback (global ) {
@@ -624,7 +626,7 @@ including callbacks, property listeners, and reducers.
624626
625627There are no parameters.
626628
627- ``` JavaScript
629+ ``` javascript
628630import { getGlobal , resetGlobal , setGlobal } from ' reactn' ;
629631
630632// Set the value.
@@ -652,7 +654,7 @@ The optional second parameter is a callback.
652654
653655` setGlobal ` with a new global state:
654656
655- ``` JavaScript
657+ ``` javascript
656658import { setGlobal } from ' reactn' ;
657659
658660// Set loading to true.
@@ -663,7 +665,7 @@ setGlobal({
663665
664666` setGlobal ` with a new global state and a callback:
665667
666- ``` JavaScript
668+ ``` javascript
667669import { setGlobal } from ' reactn' ;
668670
669671// Set loading to true.
@@ -690,7 +692,7 @@ The first parameter is a function for getting global state values.
690692The second parameter is a function for setting global state values (similar to
691693` dispatch ` ).
692694
693- ``` JavaScript
695+ ``` javascript
694696import React , { withGlobal } from ' reactn' ;
695697
696698// A button that displays the value and, when clicked, increments it.
@@ -727,7 +729,41 @@ export default withGlobal(
727729 }
728730 })
729731)(MyComponent);
732+ ```
733+
734+ ##### withInit
735+
736+ In some cases (such as when using Next.js), you may be unable to run a setup
737+ script prior to your ReactN components mounting. In order to instantiate your
738+ global state and reducers prior to mounting, you may use the ` withInit ` Higher
739+ Order Component. This HOC will await the setting of your global state before
740+ mounting the provided Lower Order Component (e.g. ` <App /> ` ).
741+
742+ ``` javascript
743+ import React , { useDispatch , useGlobal , withInit } from ' reactn' ;
744+
745+ const INITIAL_REDUCERS = {
746+ addOne : ({ count }) => ({
747+ count: count + 1 ,
748+ }),
749+ };
750+
751+ const INITIAL_STATE = {
752+ count: 0 ,
753+ };
730754
755+ export default withInit (
756+ INITIAL_STATE ,
757+ INITIAL_REDUCERS ,
758+ )(function App () {
759+ const addOne = useDispatch (' addOne' );
760+ const [ count ] = useGlobal (' count' );
761+ return (
762+ < button onClick= {addOne}>
763+ Count: {count}
764+ < / button>
765+ );
766+ });
731767```
732768
733769## Support
0 commit comments