Skip to main content

Local state

Creating and using local state

When a state is used by only one component, and maybe its children, it is recommended to use local state instead of global state. In this case useHookstate behaves similarly to React.useState, but the returned instance of State has more features.

Counter value: 0

import React from 'react';
import { useHookstate } from '@hookstate/core';
export const ExampleComponent = () => {
const state = useHookstate(0);
return <>
<b>Counter value: {state.get()} </b>
<button onClick={() => state.set(p => p + 1)}>Increment</button>
</>
}

Read more about useHookstate and State in the API reference.