|
Download react-use/docs/useMethods.md from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 965 Bytes
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/react-use/docs/useMethods.md
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/react-use/docs/useMethods.md
-
curl -L -o useMethods.md https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/react-use/docs/useMethods.md
965 Bytes
useMethods
React hook that simplifies the useReducer implementation.
Usage
import { useMethods } from 'react-use';
const initialState = {
count: 0,
};
function createMethods(state) {
return {
reset() {
return initialState;
},
increment() {
return { ...state, count: state.count + 1 };
},
decrement() {
return { ...state, count: state.count - 1 };
},
};
}
const Demo = () => {
const [state, methods] = useMethods(createMethods, initialState);
return (
<>
<p>Count: {state.count}</p>
<button onClick={methods.decrement}>-</button>
<button onClick={methods.increment}>+</button>
</>
);
};
Reference
const [state, methods] = useMethods(createMethods, initialState);
createMethods— function that takes current state and return an object containing methods that return updated state.initialState— initial value of the state.