React, but with built-in global state management.
Setter
function type for setting the global state. #155 (Thanks @rolandzwaga!)useGlobal()
hook with no parameters would sometimes not trigger a re-render on state changes after the first. #150 (Thanks @m4ttheweric!)act
. 👍
act
. 😢componentDidUnmount
for class components. #134useGlobal
's setter function. #123setGlobal
and returns the new global state, allowing you to await
the change. This is the first step towards sagas. #116function myReducer(global, dispatch, ...params) {
await dispatch({ change: 1 }); // <-- dispatch is a function
await disaptch.someOtherReducer(params); // <-- dispatch is a map
await dispatch({ change 2 });
}
withGlobal
using the default global state instead of a Provider's global state for Components inside a Provider
in versions of React >=16.3 <16.6.Upgraded react-testing-library
to @testing-library/react
.
Added documentation for useDispatch
and useGlobal
to the README.
withInit
HOC that initializes the global state and its reducers. #84
addReducer
now match their definitions in reactn/default
's Reducers
interface. #105
useDispatch
, when providing a property reducer and property name, can now be destructured. #90
useReducer
behavior.const [ value, dispatch ] = useDispatch(propertyReducerFunction, propertyName);
PropertyDispatcher
type has been added to reactn/types/dispatcher
, referencing a dispatcher that can be destructured.Riccardo#9449
!)useDispatch
with a function. This behaves closely to how React's native useReducer
works. #82import React, { useDispatch, useGlobal } from 'reactn';
const INITIAL_COUNT= 0;
setGlobal({ count: INITIAL_COUNT});
const doMath = (count, action) {
switch (action.type) {
case 'ADD' :
return count + action.value;
case 'SUBTRACT':
return count - action.value;
case 'RESET':
return INITIAL_COUNT;
default:
return count;
}
};
function MyComponent() {
const [ count] = useGlobal('count');
const dispatchMath = useDispatch(doMath, 'count'); // <-- use doMath to modify count
return <>
<button onClick={() => dispatchMath({ type: 'ADD', value: 1 })}>
Add 1
</button>
<button onClick={() => dispatchMath({ type: 'SUBTRACT', value: 3 })}>
Subtract 3
</button>
<button onClick={() => dispatchMath({ type: 'RESET' })}>
Reset
</button>
<strong>Count:</strong> {count}
</>;
}
useDispatch
parameters in README not having been updated to include dispatch
in 2.x. #87 (Thanks @yezyilomo!)The follow breaking change to withGlobal
was not deemed worthy of a major version bump, because it should have been included in 2.0.0
.
getter
and setter
function parameters are no longer of type (global, props) => ...
and (setGlobal, props) => ...
respectively.
dispatch
object as a second parameter, which contains and dispatches your global reducers.getter
function is now of type (global, dispatch, props) => ...
.setter
function is now of type (global, dispatch, props) => ...
.Before:
export default withGlobal(
(global, props) => ({ ... }),
(setGlobal, props) => ({ ... }),
)(MyComponent);
After:
export default withGlobal(
(global, dispatch, props) => ({ ... }),
(setGlobal, dispatch, props) => ({ ... }),
)(MyComponent);
withGlobal
is now fixed on React Native when there is no Provider
. #78 (Thanks @Brianop, @BDQ!)
withGlobal
to validate that it works with a Context, without a Context, and via a Provider. #66Provider.withGlobal()
required parameters, when they are optional.'reactn/types/provider'
.useGlobal
types to 'reactn/types/use-global'
.withGlobal
types to 'reactn/types/with-global'
.