Skip to content

Commit

Permalink
Adding withContext
Browse files Browse the repository at this point in the history
  • Loading branch information
pfgray committed Apr 9, 2019
1 parent b1f50c3 commit 78dee32
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
42 changes: 18 additions & 24 deletions demo/demos/ContextDemo.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import * as React from 'react';
import { fromRenderProp } from '../../src/ChainableComponent';
// how to create a context hoc?
import Step from '../Step';
import { withContext } from '../../src/lib/withContext';

const { Consumer, Provider } = React.createContext("Default Value");

const withContext = fromRenderProp(Consumer);

const DisplayContext =
withContext.render(
withContext(Consumer).render(
context => {
return (
<span>
Expand All @@ -31,28 +28,25 @@ const ContextDemo = () => (
export default () => (
<Step title="React 16 Context Demo">
<pre className='code-sample'>
{`import { fromRenderProp } from 'chainable-components';
const { Consumer, Provider } = React.createContext("Default Value");
const withStringContext = fromRenderProp(Consumer);
const DisplayContext =
withStringContext({ children: a => '' }).render(
context => {
return (
<span>
Current context is:
<pre>{JSON.stringify(context, null, 2)}</pre>
</span>
);
}
{`const DisplayContext =
withContext(Consumer).render(
context => {
return (
<span>
Current context is:
<pre>{JSON.stringify(context, null, 2)}</pre>
</span>
);
}
);
const ContextDemo = () => (
<div>
{DisplayContext}
<Provider value="Overriden value">
<div>
{DisplayContext}
</Provider>
</div>
<Provider value="Overriden value">
{DisplayContext}
</Provider>
</div>
);`}
</pre>
<ContextDemo />
Expand Down
6 changes: 6 additions & 0 deletions src/lib/withContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Consumer } from 'react';
import { ChainableComponent, fromRenderProp, RenderPropsProps } from '../ChainableComponent';

export function withContext<A>(consumer: Consumer<A>): ChainableComponent<A> {
return fromRenderProp<A>(consumer)
}

0 comments on commit 78dee32

Please sign in to comment.