From the draft-js documention, one can (in vanilla React, with no typescript) setup the Draft-js environment thus, noticing that the onChange property can be declared directly in the constructor:
import React from 'react';
import ReactDOM from 'react-dom';
import {Editor, EditorState} from 'draft-js';
class MyEditor extends React.Component {
constructor(props) {
super(props);
this.state = {editorState: EditorState.createEmpty()};
this.onChange = (editorState) => this.setState({editorState});
}
render() {
const {editorState} = this.state;
return <Editor editorState={editorState} onChange={this.onChange} />;
}
}
However, when I try to do the same with Typescript/React (code below), I get this error
error TS2339: Property 'onChange' does not exist on type 'Main'.
class Main extends React.Component<MainProps, MainState> {
constructor(props) {
super(props);
this.state = { todos: [], editorState: EditorState.createEmpty() };
this.onChange = (editorState) => this.setState({ editorState });
}
I also tried adding onChange as a property to the props
interface MainProps {
model: Model;
onChange: Function;
}
What is the appropriate way to declare such a function property in typescript/react?
onChangeas a declared method of the class?Main?this.propertyNameseems to be the way to declare a property in the constructor (from what I've seen in docs) can you show me exactly what you mean if it's different