I have a Stackblitz here
I'm simply trying to update the useState in one component from another component.
Its sort of working here but doesn't work at all in my app
The app.tsx is like
import * as React from 'react';
import Child from './Child';
import './style.css';
const App = () => {
const [value, setValue] = React.useState('Red');
return (
<div>
<div>{value}</div>
<div>
<Child onNameChange={setValue} />
</div>
</div>
);
};
export default App;
And the child component like
import * as React from 'react';
import './style.css';
const Child = ({ onNameChange }) => {
const handleInputChange = React.useCallback(() => {
onNameChange('blue');
}, [onNameChange]);
return (
<div>
<form onSubmit={handleInputChange}>
<input type="submit" value="Submit" />
</form>
</div>
);
};
export default Child;
The problems here are it keeps reloading after the button is pressed.
And in App.tsx it errors on <Child onNameChange={setValue} />
with 'Child' cannot be used as a JSX component. Its return type 'void' is not a valid JSX element.
Can anyone explain how to get this working without these mistakes