A clean way to manage the state and callbacks of your react function component!
npm install rfc-state --save
- Tear off the callbacks from the function component.
- Centralized state.
- Memorized callbacks(for better performance).
- Fully typescript-ready.
- No dependencies except
tslib
A quick example
// ./actions.ts
import { takeProps, takeState, putState } from 'rfc-state/effects';
export function changeTitle() {
return putState({ title: 'rfc-state is awesome!' });
}
// ./index.tsx
import useRfcState from 'rfc-state';
import * as rawActions from './actions';
function MyComponent(props) {
const { state, actions } = useRfcState({ title: 'using rfc-state' }, rawActions, props);
return (
<div>
<h2>{state.title}</h2>
<button onClick={actions.changeTitle}>Change Title</button>
</div>
);
}
const { state, actions, setState, getState } = useRfcState(initialState, rawActions, props);
arguments:
initialState
object or function. just likeReact.useState
rawActions
the callbacks that will be used in your function componentprops
the props
returns:
Note it's an object instead of an array!
state
the stateactions
turned from passedrawActions
. each action now has the capacity to change the state.The return type of Generator functions will be changed to Promise to indicate if the generator is done.
setState
getState
works just as they literally tells, and stay unchanged so it's safe to nest them in your callbacks.
import { takeProps, takeState, putState, isAlive } from 'rfc-state/effects';
by leveraging those effects
, your actions will be able to read the current state
and props
of your function component, and also change the state
.
takeProps
read the currentprops
takeState
read the currentstate
putState
set stateisAlive
to check if the function component is alive or not
actions
the key and magic parts of rfc-state
. let's lean about how to define all kinds of your actions.
NOTE:
- The async function will be turned into sync function that return a promise.
- The return type of Generator functions will be changed to Promise to indicate if the generator is done.
export function changeTitle() {
return putState({ title: 'rfc-state is awesome!' });
}
rfc-state
will wait until the promise to be resolved, and change the state.
export function getData() {
return fetch('/getData')
.then(res => res.json())
.then(data => {
return pustState({ data });
});
}
export async function getData() {
const data = await fetch('/getData').then(res => res.json());
return pustState({ data });
}
now we need to utilize the Generation Function.
export function* getData() {
const props: IProps = yield takeProps();
const props: IState = yield takeState();
yield putState({ loading: true });
const data = yield fetch('/getData?id=' + props.id).then(res => res.json());
yield pustState({ data });
// or
// yield fetch('/getData?id=' + props.id)
// .then(res => res.json())
// .then(res => {
// return pustState({ data });
// });
yield putState({ loading: false });
}
export async function* getData() {
const props: IProps = yield takeProps();
const data = await fetch('/getData?id=' + props.id).then(res => res.json());
yield pustState({ data });
}
MIT