Skip to content

Instantly share code, notes, and snippets.

@wkmike
Created April 7, 2024 17:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wkmike/e3787e498dd7288e990a96d2cfe5ec3b to your computer and use it in GitHub Desktop.
Save wkmike/e3787e498dd7288e990a96d2cfe5ec3b to your computer and use it in GitHub Desktop.
import React, { useState, useMemo } from "react";
function MyInput(props) {
return (
<input
value={props.value}
onInput={(e) => {
props.onInput(e.target.value);
}}
/>
);
}
function MyInputB(props) {
return (
<input
value={props.value}
onInput={(e) => {
props.onInput(e.target.value);
}}
/>
);
}
export function App(props) {
const [inputValue, setInputValue] = useState({
a: 1,
b: 2,
});
const renderInput = useMemo(() => {
return (
<MyInput
value={inputValue.a}
onInput={(value) =>
setInputValue((prevInputValue) => ({
...prevInputValue,
a: value,
}))
}
/>
);
}, [inputValue.a]);
const renderInputB = useMemo(() => {
return (
<MyInputB
value={inputValue.b}
onInput={(value) =>
setInputValue((prevInputValue) => ({
...prevInputValue,
b: value,
}))
}
/>
);
}, [inputValue.b]);
return (
<div className="App">
{renderInput}
{renderInputB}
</div>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment