@@ -17,30 +17,21 @@ yarn add create-use-context
1717## Usage
1818
1919``` javascript
20- import React , { useState } from ' react' ;
20+ import React , { Component , useState } from ' react' ;
2121
22- import createUseContext from ' create-use-context' ;
22+ import { createUseContext , createWithContext } from ' create-use-context' ;
2323
24- const [
25- MyContext,
26- MyContextProvider,
27- MyContextConsumer,
28- useMyContext,
29- ] = createUseContext ((Provider ) => ({ children }) => {
24+ const {
25+ Context : MyContext ,
26+ ContextProvider : MyContextProvider ,
27+ ContextConsumer : MyContextConsumer ,
28+ useContext : useMyContext ,
29+ } = createUseContext ((Provider ) => ({ children }) => {
3030 const [counter , setCounter ] = useState (0 );
3131
3232 return < Provider value= {{ counter, setCounter }}> {children}< / Provider> ;
3333});
3434
35- function App () {
36- return (
37- < MyContextProvider>
38- < ComponentWithHook / >
39- < ComponentWithConsumer / >
40- < / MyContextProvider>
41- );
42- }
43-
4435function ComponentWithHook () {
4536 const { counter , setCounter } = useMyContext ();
4637
@@ -71,5 +62,40 @@ function ComponentWithConsumer() {
7162 );
7263}
7364
65+ // You might also need a High Order Component to wrap your class Components
66+ // if you don't line Render Prop pattern with a Consumer Component.
67+
68+ // So you can use `createWithContext` helper function:
69+
70+ const withMyContext = createWithContext (MyContext, ' my' );
71+
72+ class ClassComponent extends Component {
73+ render () {
74+ const { counter , setCounter } = this .props .my ;
75+
76+ return (
77+ < button
78+ onClick= {() => {
79+ setCounter (counter + 1 );
80+ }}
81+ >
82+ {counter} - add one
83+ < / button>
84+ );
85+ }
86+ }
87+
88+ const ClassCompoenntWithHOC = withMyContext (ClassComponent);
89+
90+ function App () {
91+ return (
92+ < MyContextProvider>
93+ < ComponentWithHook / >
94+ < ComponentWithConsumer / >
95+ < ClassCompoenntWithHOC / >
96+ < / MyContextProvider>
97+ );
98+ }
99+
74100export default App ;
75101```
0 commit comments