Skip to content
This repository has been archived by the owner on Feb 17, 2019. It is now read-only.

Commit

Permalink
✨ add Store context with basic example
Browse files Browse the repository at this point in the history
  • Loading branch information
balazsorban44 committed Jan 27, 2019
1 parent 9c4e6cd commit b8b8e94
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 2 deletions.
18 changes: 17 additions & 1 deletion src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,36 @@ import './App.css'

import {Trans, withNamespaces} from 'react-i18next'
import LanguageChooser from './LanguageChooser'
import {withStore} from './db'
import initValues from './db/initialValues.json'


const App = ({t}) =>
<div className="App">
<header className="App-header">
<LanguageChooser/>
<TestStoreContext/>
<h1 className="App-title">
{t('welcome.title', {framework: "react-i18next"})}
</h1>
<Trans i18nKey="welcome.intro">
To get started, edit <code>src/App.js</code> and save to reload.
</Trans>
<LanguageChooser/>
</header>
</div>

export default withNamespaces('common')(App)


const TestStoreContext = withStore(({store}) =>
<div>
<h3>Testing store context</h3>
<button onClick={() => store.handleHelloWorld(store.value)}>Click me!</button>
<input autoFocus onChange={e => store.handleChangeValue(e.target.value)} value={store.value}/>
{
initValues.value !== store.value &&
<button onClick={() => store.handleChangeValue("")}>Clear</button>
}
<hr/>
</div>
)
33 changes: 33 additions & 0 deletions src/db/Store.js
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
16 changes: 16 additions & 0 deletions src/db/actions/helloWorld.js
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})
}
5 changes: 5 additions & 0 deletions src/db/index.js
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}
3 changes: 3 additions & 0 deletions src/db/initialValues.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"value": "This is an initial value"
}
14 changes: 14 additions & 0 deletions src/db/withStore.js
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
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import ReactDOM from 'react-dom'
import './index.css'
import App from './App'

import {Database} from './db'

import i18next from "./lib/i18next"
import {I18nextProvider} from 'react-i18next'
Expand All @@ -11,7 +12,9 @@ import * as serviceWorker from './serviceWorker'

ReactDOM.render(
<I18nextProvider i18n={i18next}>
<App/>
<Database>
<App/>
</Database>
</I18nextProvider>,
document.getElementById('root')
)
Expand Down

0 comments on commit b8b8e94

Please sign in to comment.