I am work in react + typescript for first time.
interface IState {
stateval: number;
}
class Forgotpassword extends React.Component<any, IState> {
constructor(props: any){
super(props);
this.state = { stateval: 1 };
}
public submit() {
this.setState({ stateval:2 });
}
public render() {
const { stateval} = this.state;
return (
<div className="App">
<Button onClick={this.submit}>Send OTP</Button>
</div>
);
}
}
When i click the submit button it throws
Uncaught TypeError: this.setState is not a function
this.submit = this.submit.bind(this);to your constructor.<Button onClick={() => this.submit}>Send OTP</Button>orpublic submit = () => { this.setState({ stateval:2 }); }thiscontext of the button, is the button. you need to bind the submit function to the ForgotPassword class