useRdxReset(state)

会根据当前传入的state参数来返一个函数,如果state 是一个atom,则调用该函数将状态重置为默认值,如果是一个compute,则会 调用compute的set方法,并传入DEFAULT_VALUE。


function useRdxReset<GModel>(state: RdxState<GModel>): () => void
  • state: 可是是一个 atom 或者是一个 compute.

Root

The fallback content to display on prerendering
import React from 'react';
import {
atom,
useRdxSetter,
RdxContext,
useRdxState,
compute,
isLoading,
isDefaultValue,
useRdxReset,
} from '@alife/rdx';
const Atom = atom({
id: 'atom',
defaultValue: 1,
});
const Compute = compute({
id: 'compute',
get: ({ get }) => {
return get(Atom) * 10;
},
set: ({ set }, newValue) => {
if (isDefaultValue(newValue)) {
set(Atom, 100);
} else {
set(Atom, newValue);
}
},
});
export const Root = () => {
return (
<RdxContext>
<Counter />
</RdxContext>
);
};
const Counter = () => {
const [state, setState] = useRdxState(Atom);
const [computeState, setComputeState] = useRdxState(Compute);
const reset = useRdxReset(Atom);
const resetCompute = useRdxReset(Compute);
return (
<span style={{ display: 'inline-flex', justifyContent: 'space-around' }}>
<div>
Atom:{' '}
<input
value={state}
onChange={(event: any) => setState(event.target.value)}
/>
Compute:{' '}
<input
value={computeState}
onChange={(event: any) => setComputeState(event.target.value)}
/>
</div>
<button
onClick={() => {
reset();
}}
>
重置Atom
</button>
<button
onClick={() => {
resetCompute();
}}
>
重置Compute
</button>
</span>
);
};