-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor to use only react context + render props (#1)
Gets rid of the singleton store and observable dependency in favor of React context + nesting The most basic way to use this is just using a render prop that accepts state + setState and is almost the same as State from react-powerplug, but the whitespace limitations make it very difficult to work with. Providing the values as context seems cleaner for more complex documents even if it requires more local nesting. See demo/counter-child-function.mdx.
- Loading branch information
1 parent
3cdf3b2
commit 1074a1e
Showing
8 changed files
with
165 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { State, Observe } from '../src'; | ||
|
||
# Counter | ||
|
||
<State initialState={{ count: 0 }}> | ||
{({ setState, ...state }) => { | ||
// cannot leave blank lines here | ||
// but at least we can use JS variables because we are inside a function! | ||
const increment = 1; | ||
// no blank lines here either | ||
// you can use comments or a single ; | ||
return ( | ||
// the body of the document can be put inside the render function | ||
// this has to be wrapped in a fragment if it has multiple nodes | ||
// | ||
// code inside the block cannot be indented >= 4 spaces or it will be | ||
// rendered as text instead of parsed | ||
<React.Fragment> | ||
|
||
# H1 | ||
|
||
<button onClick={() => setState(s => ({ count: s.count + increment }))}> | ||
Click me | ||
</button> | ||
|
||
The button has been clicked: | ||
|
||
<span>{state.count} times</span> | ||
|
||
</React.Fragment> | ||
)}} | ||
</State> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { Init, Observe } from '../src/index'; | ||
import { State, Observe } from '../src/index'; | ||
import Table from './helpers/table'; | ||
|
||
<link href="https://unpkg.com/[email protected]/github-markdown.css" rel="stylesheet" /> | ||
|
@@ -11,19 +11,18 @@ There a number of factors to consider when buying a car. | |
|
||
Use our special formula to help you decide! | ||
|
||
<Init | ||
state={{ | ||
<State | ||
initialState={{ | ||
vehicles: [ | ||
{ name: "Volvo", price: 29000, reliability: 7 }, | ||
{ name: "Honda", price: 25000, reliability: 8 }, | ||
], | ||
revealChoice: false, | ||
}} | ||
key="simple-example" | ||
/> | ||
> | ||
|
||
<Observe> | ||
{({ state: {vehicles} }) => ( | ||
{({vehicles}) => ( | ||
<Table | ||
data={[Object.keys(vehicles[0]), ...vehicles.map(v => Object.values(v))]} | ||
/> | ||
|
@@ -34,7 +33,7 @@ Which should you buy? | |
|
||
<Observe> | ||
{ | ||
({state: {vehicles, revealChoice}, setState}) => { | ||
({vehicles, revealChoice, setState}) => { | ||
const mostReliable = [...vehicles] | ||
.sort((a, b) => b.reliability - a.reliability)[0]; | ||
const handleClick = e => { | ||
|
@@ -53,4 +52,6 @@ Which should you buy? | |
} | ||
</Observe> | ||
|
||
</State> | ||
|
||
</article> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.