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
commsTitlestate. What or how are you verifying/validating that state isn't updating? In the least you should see the updated state in the input when you are typing. What isduplicateHubDatain the second component?duplicateHubData.data[0]to begin with. Not too sure if something like that needs to wrapped in a useEffect or something so that it renders?duplicateHubData.data[0]is and where it comes from? Can you also share how the two component snippets relate?DuplicateHubDisplayand being rendered by some other component 1?duplicateHubData.data[0]is the input state from Component 1. Basically im trying to replicate Component 1's input value to Component 2's input. The second method by passing in thevalue={duplicateHubData.data[0]}replicates Component 1's value, but it doesn't allow me to make edits. the third method by using state, doesn't even show Component 1s input value however it allows me to update the values