@airma/react-state
Advanced tools
Comparing version 15.2.3 to 15.2.4
@@ -49,6 +49,11 @@ // src/index.ts | ||
} | ||
function useRefreshModel(model, state) { | ||
const [, agent] = useTupleModel(model, state, { refresh: true }); | ||
return agent; | ||
} | ||
export { | ||
useControlledModel, | ||
useModel, | ||
useRefreshModel, | ||
useTupleModel | ||
}; |
import { AirModelInstance, AirReducer } from '@airma/core'; | ||
export declare type Option = { | ||
refresh?: boolean; | ||
}; | ||
export declare function useModel<S, T extends AirModelInstance, D extends S>( | ||
model: AirReducer<S, T>, | ||
state: D | ||
state: D, | ||
option?: Option | ||
): T; | ||
@@ -12,8 +17,13 @@ | ||
D extends S | ||
>(model: AirReducer<S, T>, state: D): [S, T]; | ||
>(model: AirReducer<S, T>, state: D, option?: ((s: S) => any) | Option): [S, T]; | ||
export function useControlledModel< | ||
S, | ||
T extends AirModelInstance, | ||
D extends S | ||
>(model: AirReducer<S, T>, state: D, onChange: (s: S) => any): T; | ||
export function useControlledModel<S, T extends AirModelInstance, D extends S>( | ||
model: AirReducer<S, T>, | ||
state: D, | ||
onChange: (s: S) => any | ||
): T; | ||
export function useRefreshModel<S, T extends AirModelInstance, D extends S>( | ||
model: AirReducer<S, T>, | ||
state: D | ||
): T; |
{ | ||
"name": "@airma/react-state", | ||
"version": "15.2.3", | ||
"version": "15.2.4", | ||
"module": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "typings": "index.d.ts", |
@@ -74,2 +74,3 @@ [![npm][npm-image]][npm-url] | ||
* state - this is the default state for model initialization. | ||
* option - this is an optional param, you can set it `{refresh: true}` to make param state change always affect current model as a new state. There is a more easy API `useRefreshModel` | ||
@@ -85,3 +86,4 @@ returns modelInstance which is generated by calling `model(state)`. Everytime when a method from modelInstance is called, the result of this method is treated as a next state param, and recalls `model(state)` to refresh modelInstance. | ||
model: AirReducer<S, T>, | ||
state: D | ||
state: D, | ||
option?: {refresh:boolean} | ||
): T | ||
@@ -94,3 +96,3 @@ ``` | ||
* state - this is the default state for model initialization. | ||
* onChange - this is an optional callback, which can drive `useTupleModel` to a uncontrolled mode, and make state update by the param state change, you can see how to use it in `useUncontrolledModel` API. | ||
* onChangeOrOption - this is an optional param. If it is a callback, `useTupleModel` goes to a controlled mode, it only accepts state change, and uses `onChange` callback to change next state out, you can use `useControlledModel` to do this too. If it is an option config, you can set `{refresh: true}` to make param state change always affect current model as a new state. | ||
@@ -107,3 +109,3 @@ returns the current param state and modelInstance, like `[state, instance]`. | ||
state: D, | ||
onChange?:(s:S)=>any | ||
onChangeOrOption?: ((s:S)=>any)|{refresh:boolean} | ||
): [S, T] | ||
@@ -194,2 +196,16 @@ ``` | ||
### useRefreshModel | ||
* model - model generate function, it accepts a state param, and returns a model object, which contains methods for generating next state and any other properties for describing state. | ||
* state - this is the state outside for model. When this param changes, the model refreshes with it as a new state. | ||
returns a model instance like `useModel`, but can be refreshed by state param too. | ||
```ts | ||
export function useRefreshModel<S, T extends AirModelInstance, D extends S>( | ||
model: AirReducer<S, T>, | ||
state: D | ||
): T; | ||
``` | ||
## Tips | ||
@@ -243,5 +259,5 @@ | ||
## Safe reduce state | ||
## Secure reduce state | ||
The `useModel` and `useTupleModel`(without onChange) APIs are safing for usage. The state is outside of react system, so every update from methods is a safe reducing process. If you want to use `useState` to replace its job, you have to call it like: `setState((s)=>s+1)`. | ||
The API from `useTupleModel`(without onChange) like `useModel`, `useRefreshModel` are secure for state update. The state is outside of react system, so every update from methods is a secure reducing process. If you want to use `useState` to replace its job, you have to call it like: `setState((s)=>s+1)`. | ||
@@ -248,0 +264,0 @@ ## typescript check |
12842
81
270