Skip to content

Instantly share code, notes, and snippets.

@chathuranga94
Created July 29, 2019 18:25
Show Gist options
  • Save chathuranga94/dcaf1cdf3cb6d0e4f6b095fa7f5fad00 to your computer and use it in GitHub Desktop.
Save chathuranga94/dcaf1cdf3cb6d0e4f6b095fa7f5fad00 to your computer and use it in GitHub Desktop.
import React from 'react';
import { GlobalStoreProvider, GlobalStore } from 'global-store-hook';
const App = () => {
const init = {
lang: 'en',
color: 'blue'
}
return (
<GlobalStoreProvider initValues={init}>
<LanguagePicker />
<Menu />
</GlobalStoreProvider>
);
}
const Menu = () => <MenuItem />;
const LanguagePicker = () => {
const store = GlobalStore();
return (
<div>
<button onClick={() => store.set('lang', 'en')}>English</button>
<button onClick={() => store.set('lang', 'fr')}>French</button>
</div>
);
}
const MenuItem = () => {
const store = GlobalStore();
return (
<div>
<p>Theme colour: {store.get('color')}</p>
<p>Locale: {store.get('lang')}</p>
</div>
);
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment