I'm trying to use the code from react.js docs about forms, but I'm using it with typescript, the code is on this link but when I added it to the typescript file I got an error Property 'value' does not exist on type 'object'.
class MailListSubscribe extends React.Component<
IMaterialUIComponentProps,
object
> {
constructor(props: any) {
super(props);
this.state = {value: ''};
this.handleSubmit = this.handleSubmit.bind(this);
}
handleSubmit(event: any) {
// error appears on this.state.value
alert('A name was submitted: ' + this.state.value);
event.preventDefault();
}
public render() {
return (
<form onSubmit={this.handleSubmit}>
<label>
Name:
// error appears again on this.state.value
<input type="text" value={this.state.value} onChange={this.handleChange} />
</label>
<input type="submit" value="Submit" />
</form>
);
Any help is appreciated, thanks!
objectinReact.Component<>? I don't think it's necessary and probably it's what causes your problem. When I have to extend a component in Typescript I usually passReact.Component<PropsType, StateType>and then in the costructor I initialize the state defined in the StateTypeReact.Component<IMaterialUIComponentProps>. I think that's enough. On the contrary, if you need to define a state, I suggest you to define aIStateinterface on top of you component (out of the class I mean) and pass it to your component as you did for object:React.Component<IMaterialUIComponentProps, IState>