Im running into an issue where the onChange={} on <input /> doesn't change the value through state.
I have two Components, Component 1 passes the the state of an input value to Component 2 as a prop:
Component 1:
function MainContent() {
const [inputValue, setInputValue] = React.useState("");
const onChangeHandler = (event) => {
setInputValue(event.target.value);
};
return (
<>
<input
id="shtitle"
value={inputValue}
onChange={onChangeHandler}
className="input sh-input"
type="text"
placeholder="Incident Title"
/>
<DuplicateHubDisplay
data={[
inputValue,
momentDate.format("DD/MM/YYYY HH:mm A"),
textareaValue,
]}
/>
</>
);
}
export default MainContent;
Component 2: Gets the prop data from Component 1
const [commsTitle, setCommsTitle] = useState("");
// const [commsTitle, setCommsTitle] = useState(duplicateHubData.data[0]); //Does not work
<input
id="duplicatehub-title"
value={duplicateHubData.data[0]}
className="input"
placeholder="Incident Title"
/>
I don't know how to store duplicateHubData.data[0] as a state and then use onChange to change the state value.
Doing this does not allow me to change the input value:
const DuplicateHubDisplay = (duplicateHubData) => {
const [commsTitle, setCommsTitle] = useState(duplicateHubData.data[0]);
return (
<input
id="duplicatehub-title"
onChange={(e) => {
setCommsTitle(e.target.value);
}}
value={commsTitle}
className="input"
placeholder="Incident Title"
/>
);
}
export default DuplicateHubDisplay;
Thanks in advance