DEPRECATED USE OTHER ALTERNATIVES SUCH https://recoiljs.org/
A React global state using the context and hooks API, without any dependencies.
function reducer(state, action) {
switch (action.type) {
case 'inc':
return state + 1;
case 'dec':
return state - 1;
default:
return state;
}
}
const initialValue = 10;
export const [CounterProvider, useGlobalCounter] = createGlobalReducer(
reducer,
initialValue
);
import { CounterProvider } from './hooks';
export default () => (
<CounterProvider>
<App />
</CounterProvider>
);
import { useGlobalCounter } from './hooks';
export default function Counter({ name }) {
const [state, dispatch] = useGlobalCounter();
const increment = useCallback(() => dispatch({ type: 'inc' }));
const decrement = useCallback(() => dispatch({ type: 'dec' }));
return (
<div>
<h2>{name}</h2>
{state}
<button onClick={increment}>+</button>
<button onClick={decrement}>-</button>
</div>
);
}
export const [CounterProvider, useGlobalCounter] = createGlobalReducer(
createReducer({
inc: x => x + 1,
dec: x => x - 1
}),
10,
dispatch => ({
increment: () => setTimeout(() => dispatch({ type: 'inc' }), 300),
decrement: () => dispatch({ type: 'dec' })
})
);
export default function Counter({ name }) {
const [state, , actions] = useGlobalCounter();
return (
<div>
<h2>{name}</h2>
{state}
<button onClick={actions.increment}>+</button>
<button onClick={actions.decrement}>-</button>
</div>
);
}
To install the project dependencies, run:
npm install
It installs the node_modules
dependencies.
- To run all tests, use
npm run test
. - To run the linter use, use
npm run lint
.
- To do a release, use
npm run release
.
- Use conventional commits.
- Use the present tense ("Add feature" not "Added feature").
- Use the imperative mood ("Move cursor to..." not "Moves cursor to...").
- Limit the first line to 50 characters or less.
- Reference issues and pull requests explicitly.