Package Exports
- @blue-tomato/use-retext
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@blue-tomato/use-retext) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
🔐 React useReducer alternative by Blue Tomato.
Installation
Type npm install @blue-tomato/use-retext
into your console
Usage
import React from 'react';
import useRetext from '@blue-tomato/use-retext';
import SideMenu from './SideMenu';
const store = {
state: {
isOpen: false,
searchTerm: undefined,
},
reducer: {
setSearchTerm: (state, payload) => ({ ...state, searchTerm: payload }),
sideMenu: {
toggle: state => ({ ...state, isOpen: !state.open }),
},
},
};
export default ({ children, ...props }) => {
const { state, dispatch } = useRetext(store);
return (
<div {...props}>
<button
type="button"
onClick={() => {
dispatch.setSearchTerm('test');
dispatch.sideMenu.toggle();
}}
>
Test me!
</button>
<SideMenu state={state} />
</div>
);
};
Usage with createStore
Store.js
import { createStore } from '@blue-tomato/use-retext';
const store = {
state: {
isOpen: false,
searchTerm: undefined,
},
reducer: {
setSearchTerm: (state, payload) => ({ ...state, searchTerm: payload }),
sideMenu: {
toggle: state => ({ ...state, isOpen: !state.open }),
},
},
};
const [Store, useStore] = createStore(store);
export default Store;
export { useStore };
Parent.jsx
import React from 'react';
import Store from './Store';
import Child from './Child';
export default ({ ...props }) => (
<Store {...props}>
<Child />
</Store>
);
Child.jsx
import React from 'react';
import { useStore } from './Store';
export default ({ ...props }) => {
const { state, dispatch } = useStore();
return (
<div {...props}>
<button
type="button"
onClick={() => {
dispatch.setSearchTerm('test');
}}
>
Test me!
</button>
<div>Search term: {state.searchTerm}</div>
</div>
);
};