Looks like wrapping the make
function definition in React.memo
is indeed sufficient. For example, in the following code, changing the Input
’s text will only rerender Counter
if the first letter is changed:
module Counter = {
@react.component
let make = React.memo((~prefix) => {
let (count, setCount) = React.useState(() => 0)
Console.log("Counter rendered")
<div>
<p>{String.concat(prefix, Int.toString(count))->React.string}</p>
<button onClick={_ => setCount(c => c + 1)}>{"Increase"->React.string}</button>
</div>
})
}
module Input = {
@react.component
let make = () => {
let (text, setText) = React.useState(() => "")
Console.log("Input rendered")
<div>
<input onChange={c => setText(_ => JsxEventU.Form.target(c)["value"])} value={text}/>
<Counter prefix={text->String.slice(~start=0, ~end=1)} />
</div>
}
}