ReasonReact

ReasonReact

  • Docs
  • Try
  • Examples
  • Community
  • Blog
  • Languages iconEnglish
    • 日本語
    • Español
    • Français
    • 한국어
    • Português (Brasil)
    • Русский
    • 中文
    • 繁體中文
    • Help Translate
  • GitHub

›Recipes & Snippets

Recipes & Snippets

  • A List of Simple Examples
  • useState Hook
  • useReducer Hook
  • useEffect Hook
  • useState, useEffect in a Form
  • A Custom useDebounce Hook
  • Adding data-* attributes
  • Working with Optional Data
  • Render Props
  • Importing JS into Reason
  • Importing Reason into JS
  • ReasonReact using ReactJS
  • ReactJS using ReasonReact
  • Gentype & Typescript
  • Example Projects
  • GraphQL & Apollo
  • Styling: Tailwind CSS
Edit

useEffect Hook

React.js docs for useEffect

Here's a simple example of how to use React's useState with useEffects.

Cleaning up an Effect

[@react.component]
let make = () => {
    React.useEffect0(() => {
        let id = subscription.subscribe();
        /* clean up the subscription */
        Some(() => subscription.unsubscribe(id));
    });
}

Conditionally Firing an Effect

With this, the subscription will only be recreated when ~source changes

[@react.component]
let make = (~source) => {
    React.useEffect1(() => {
        let id = subscription.subscribe();
        /* clean up the subscription */
        Some(() => subscription.unsubscribe(id));
    }, [|source|]);
}
← useReducer HookuseState, useEffect in a Form →