This repository has been archived by the owner on Feb 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ add Store context with basic example
- Loading branch information
1 parent
9c4e6cd
commit b8b8e94
Showing
7 changed files
with
92 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import React, {Component} from "react" | ||
import helloWorld, {changeValue} from "./actions/helloWorld" | ||
import initValues from "./initialValues.json" | ||
|
||
const Store = React.createContext() | ||
|
||
export class Database extends Component { | ||
|
||
state = { | ||
...initValues | ||
} | ||
|
||
|
||
helloWorld = helloWorld.bind(this) | ||
|
||
changeValue = changeValue.bind(this) | ||
|
||
render() { | ||
return ( | ||
<Store.Provider | ||
value={{ | ||
handleHelloWorld: this.helloWorld, | ||
handleChangeValue: this.changeValue, | ||
...this.state | ||
}} | ||
> | ||
{this.props.children} | ||
</Store.Provider> | ||
) | ||
} | ||
} | ||
|
||
export default Store |
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,16 @@ | ||
import initValues from "../initialValues.json" | ||
|
||
/** | ||
* Test function | ||
*/ | ||
export default function helloWorld() { | ||
alert(this.state.value) | ||
} | ||
|
||
/** | ||
* Change store value | ||
* @param {string} value | ||
*/ | ||
export function changeValue(value) { | ||
this.setState({value: value === "" ? initValues.value : value}) | ||
} |
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,5 @@ | ||
import withStore from "./withStore" | ||
import Store, {Database} from "./Store" | ||
|
||
export default Store | ||
export {withStore, Database} |
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,3 @@ | ||
{ | ||
"value": "This is an initial value" | ||
} |
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,14 @@ | ||
import React from 'react' | ||
import Store from '.' | ||
|
||
/** | ||
* HOC implementation of Store context | ||
* @param {Component} WrappedComponent The component to pass the store values to | ||
* @returns {Component} new component with the Store values in this.props.store | ||
*/ | ||
const withStore = WrappedComponent => props => | ||
<Store.Consumer> | ||
{store => <WrappedComponent {...props } store={store}/>} | ||
</Store.Consumer> | ||
|
||
export default withStore |
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