@lowinc/quellingblade
v0.0.3
Published
usereducer helper
Downloads
2
Readme
QuellingBlade
堕落地精的斧头,能让你更快的在森林中穿行。
State & Handler
import blade, {useBlade} from "./src";
import {useReducer} from "react";
const initState = {
name: "lowinc",
age: 100,
};
type State = typeof initState;
const handler = {
updateName(state, payload: State["name"]) {
return {
...state,
name: payload,
};
},
};
Pages
通过blade创建简便的mutations和reducer
const {mutations, reducer} = blade(handler);
export default function PageInBlade() {
const [state, dispatch] = useReducer(reducer, initState);
const updateName = () => {
dispatch(mutations.updateName("LAO_SI_JI"));
};
return <button onClick={updateName}>{state.name}</button>;
}
或者直接使用useBlade代替useReducer
export default function PageInUseBlade() {
const {state, blade} = useBlade(handler, initState);
const updateName = () => {
blade.updateName("LAO_SI_JI");
};
return <button onClick={updateName}>{state.name}</button>;
}