The parent component doesn’t seem to access the ErrorMessage I get this error…
Objects are not valid as a React child (found: object with keys {error_msg}). If you meant to render a collection of children, use an array instead.
I checked and error_msg is the error message.
in the parent component I have…
function App() {
const [ErrorMessage, setErrorMessage] = useState('');
const handleClickUpdate = async (event) => {
...
const error_msg = Validation(postData)
setErrorMessage(error_msg)
...
}
<button type="submit" onClick={() =>
handleClickUpdate({stuff})}>Update</button>
return (
<div>
<Submit setSomethingElse={somethingElse} setErrorMessage={(error) =>setErrorMessage(error)} ErrorMessage={ErrorMessage} />
</div>
<div className="error-message"> {ErrorMessage}</div>
);
}
So in the child component:
function Submit({ setSomethingElse, setErrorMessage,
ErrorMessage }) {
const handleSubmit = async (event) => {
...
setErrorMessage('Error Message')
...
}
But errorMessage is empty even after the assignment. Am I not passing in the prop correctly?
return (
<form onSubmit={handleSubmit}>
...
<div>{ErrorMessage}</div>
</form>
);
}
2
Answers
The parent would need to pass not just the state setter, but also the state getter/value.
and then have the child handle that prop like so..
Instead of passing the function directly as a prop can you try this:
And in the child component you can just use the function like you are already doing.