Created
April 7, 2024 17:09
-
-
Save wkmike/e3787e498dd7288e990a96d2cfe5ec3b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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