I am trying to use callback that will validate an input.
When I am trying to type in the textbox, I am having the error below. Can someone help why I am having the error and how I can correct it.
Is callback not allowed? What am I missing?
Uncaught Error: Objects are not valid as a React child (found: object with keys {dispatchConfig, _targetInst, nativeEvent, type, target, currentTarget, eventPhase, bubbles, cancelable, timeStamp, defaultPrevented, isTrusted, isDefaultPrevented, isPropagationStopped, _dispatchListeners, _dispatchInstances}). If you meant to render a collection of children, use an array instead. in div (created by HelloWorldWithPropertiesES5) in HelloWorldWithPropertiesES5 (created by Parent2) in Parent2 (created by App) in div (created by App) in App at invariant (react-dom.development.js:55)
import * as React from 'react';
interface IProps{
userName?:string
}
const HelloWorldWithPropertiesES5= (props:IProps)=>{
return (
<div>
Hello {props.userName}
</div>
)
}
export default HelloWorldWithPropertiesES5;
import * as React from 'react'
interface IProps {
userName : string;
onChange : (event:any) => void;
}
export const NameEditComponent = (props : IProps) =>
<>
<h2>this shows event changes</h2>
<label>Update name:</label>
<input value={props.userName}
onChange={props.onChange}
/>
</>
export default NameEditComponent;
import * as React from 'react';
import HelloWorldWithPropertiesES5 from 'src/Lesson2-Properties/HelloES5';
import NameEditComponent from './MyChild';
interface IState {
userName : string;
}
export class Parent2 extends React.Component<{},IState> {
constructor(props:any) {
super(props);
this.state = {userName: 'defaultUserName'};
}
public setUsernameState = (newName:string) => {
this.setState({userName:newName},()=>this.validateName());
}
public validateName () {
if (this.state.userName.length === 0) {
alert('error')
}
}
public render() {
return (
<>
<HelloWorldWithPropertiesES5 userName={this.state.userName} />
<NameEditComponent userName={this.state.userName} onChange={this.setUsernameState} />
</>
);
}
}
export default Parent2;