DocsAPISyntaxReactPlaygroundBlogCommunity
  • Playground
  • Blog
  • Community
  • X
  • Bluesky
  • GitHub
  • Forum
rescript-react
Overview
  • Introduction
  • Installation
  • Migrate from JSX v3
Main Concepts
  • Elements & JSX
  • Rendering Elements
  • Components and Props
  • Arrays and Keys
  • Events
  • Refs and the DOM
  • Context
  • memo
    • arePropsEqual
  • Styling
  • Router
  • Lazy Components
  • Import / Export ReactJS
Hooks & State Management
  • Hooks & State Management Overview
  • useEffect Hook
  • useState Hook
  • useReducer Hook
  • useContext Hook
  • useRef Hook
  • Build A Custom Hook
Guides
  • Beyond JSX
  • Forwarding Refs
  • Extensions of props
Extra
  • LLMs
Docs / rescript-react / memo
Edit

memo

React.memo lets you skip re-rendering a component when its props are unchanged.

Wrap a component in memo to get a memoized version of that component. This memoized version of your component will usually not be re-rendered when its parent component is re-rendered as long as its props have not changed.

But React may still re-render it: memoization is a performance optimization, not a guarantee.
ReScriptJS Output
@react.component
let make = React.memo((~a: int, ~b: string) => {
  <div>
    {React.int(a)}
    <br />
    {React.string(b)}
  </div>
})

arePropsEqual

In React, memo can accept an optional argument called "arePropsEqual". This function takes two arguments: the previous props and the new props of the component. It should return true if the old and new props are the same, meaning the component will produce the same output and behavior with the new props as it did with the old ones.

In ReScript, you can use the arePropsEqual function with the React.memoCustomCompareProps binding. However, React.memoCustomCompareProps cannot be combined with @react.component.

To work around this, you can redefine the make binding:

ReScriptJS Output
@react.component
let make = (~disabled, ~onClick) => {
  <button
    disabled={disabled}
    onClick={ev => ev->JsxEvent.Mouse.preventDefault->onClick}>
    {React.string("My button")}
  </button>
}

let make = React.memoCustomCompareProps(make, (p1, p2) =>
  p1.disabled == p2.disabled
)

Another approach is to use a custom prop type and remove the @react.component annotation.

ReScriptJS Output
type props = {
  disabled: bool,
  onClick: JsxEvent.Mouse.t => unit,
}

let make = React.memoCustomCompareProps(
  ({disabled, onClick}) => {
    <button disabled={disabled} onClick={ev => ev->onClick}>
      {React.string("My button")}
    </button>
  },
  (p1, p2) => p1.disabled == p2.disabled,
)
ContextStyling

© 2025 The ReScript Project

Software and assets distribution powered by KeyCDN.

About
  • Community
  • ReScript Association
Find us on