0.1.1 • Published 2 years ago
react-small-simple-hooks v0.1.1
React small simple hooks
This library is example of simple usefull react hooks. Hooks list is planned to be increased.
Hooks
useCombinedState
Multiple useState per component can make it very difficult to navigate between states. Especially when they connected.
Example: One state used for loading visibility flag, second for loading render options.
import { useCombinedState } from 'react-small-simple-hooks';
function App() {
const [state, setState] = useCombinedState({ firstState: 'firstValue', secondState: 'secondValue' });
const updateFirstState = () => {
setState({ firstState: 'newFirstValue' }); // => { firstState: 'newFirstValue', secondState: 'secondValue' }
// when firstState was changed secondState stand the same.
}
const updateSecondState = () => {
setState((previousState) => {
return { secondState: `${previousState.secondState}Postifx` }
}); // => { firstState: 'firstValue', secondState: 'secondValuePostifx' }
// previous state accessible when pass function to setState
}
}