Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Creating Custom Hooks | Hooks
Introduction to React

bookCreating Custom Hooks

Custom hooks allow us to reuse stateful logic across multiple components. To create a custom hook, we simply create a function starting with use. The easiest example of a custom hook is creating a reusable toggle state:

function useToggle(initialState = false) {
  const [state, setState] = useState(initialState);
  const toggleFunction = () => setState(state => !state);
  return [state, toggleFunction];
}

function Main(props) { 
  const [ state, toggle ] = useToggle();
  return ( 
    <button onClick={toggle}>
      {state ? "Turn Off" : "Turn On"}
    </button>
  );
}

In the above code, we create our custom Hook function called useToggle. We create a state variable inside that function and assign a value of false (by default) to that state variable, apart from that, we also create an inline function that can be used for toggling the state variable's value.

We return both of these in an array where the first value is the state variable's reference, and the second is the function.

Using this custom hook, we can now create multiple independent togglable states in different (or the same) component(s) with ease:

function useToggle(initialState = false) {
  const [state, setState] = useState(initialState);
  const toggleFunction = () => setState(state => !state);
  return [state, toggleFunction];
}


function OneButton(props) {
  const [ state, toggle ] = useToggle();
  return ( 
    <button onClick={toggle}>
      {state ? "Turn Off" : "Turn On"}
    </button>
  );
}

function TwoButtons(props) {
  const [ state_1, toggle_1 ] = useToggle();
  const [ state_2, toggle_2 ] = useToggle();
  return ( 
    <div>
      <button onClick={toggle_1}>
        {state_1 ? "Turn Off" : "Turn On"}
      </button>
      <button onClick={toggle_2}>
        {state_2 ? "Turn Off" : "Turn On"}
      </button>
    </div>
  );
}

function Main(props) { 
  const [ state, toggle ] = useToggle();
  return ( 
    <div>
      <TwoButtons />
      <OneButton />
    </div>
  );
}

As you can see, each button can be independently turned off/on :

question mark

How do you create a custom hook?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 6. Kapittel 5

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Suggested prompts:

Still meg spørsmål om dette emnet

Oppsummer dette kapittelet

Vis eksempler fra virkeligheten

Awesome!

Completion rate improved to 2.7

bookCreating Custom Hooks

Sveip for å vise menyen

Custom hooks allow us to reuse stateful logic across multiple components. To create a custom hook, we simply create a function starting with use. The easiest example of a custom hook is creating a reusable toggle state:

function useToggle(initialState = false) {
  const [state, setState] = useState(initialState);
  const toggleFunction = () => setState(state => !state);
  return [state, toggleFunction];
}

function Main(props) { 
  const [ state, toggle ] = useToggle();
  return ( 
    <button onClick={toggle}>
      {state ? "Turn Off" : "Turn On"}
    </button>
  );
}

In the above code, we create our custom Hook function called useToggle. We create a state variable inside that function and assign a value of false (by default) to that state variable, apart from that, we also create an inline function that can be used for toggling the state variable's value.

We return both of these in an array where the first value is the state variable's reference, and the second is the function.

Using this custom hook, we can now create multiple independent togglable states in different (or the same) component(s) with ease:

function useToggle(initialState = false) {
  const [state, setState] = useState(initialState);
  const toggleFunction = () => setState(state => !state);
  return [state, toggleFunction];
}


function OneButton(props) {
  const [ state, toggle ] = useToggle();
  return ( 
    <button onClick={toggle}>
      {state ? "Turn Off" : "Turn On"}
    </button>
  );
}

function TwoButtons(props) {
  const [ state_1, toggle_1 ] = useToggle();
  const [ state_2, toggle_2 ] = useToggle();
  return ( 
    <div>
      <button onClick={toggle_1}>
        {state_1 ? "Turn Off" : "Turn On"}
      </button>
      <button onClick={toggle_2}>
        {state_2 ? "Turn Off" : "Turn On"}
      </button>
    </div>
  );
}

function Main(props) { 
  const [ state, toggle ] = useToggle();
  return ( 
    <div>
      <TwoButtons />
      <OneButton />
    </div>
  );
}

As you can see, each button can be independently turned off/on :

question mark

How do you create a custom hook?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 6. Kapittel 5
some-alt