javascript - Should Reacts `propTypes` and `defaultProps` be used in conjunction with Flowtype, or is Flowtype comprehensive enough? -


thinking of simple example such as:

class commentareacomponent extends react.component { static proptypes = {     id: proptypes.string.isrequired,     loading: proptypes.bool, };  static defaultprops = {     loading: false, }; 

in constructor can define achieve (i think) same thing:

class mycomponent extends react.component {     constructor({         loading = false,     }:{          id:string,          loading?:boolean      }) {         super(arguments[0]);     } } 

the second example using flowtype. using reacts proptypes , defaultprops offer advantage? or can drop them when using flowtype?

you can surely use flow types instead of react proptypes, proposed syntax not common way it. see flow docs (scroll down es6 syntax):

class button extends react.component {   props: {     title: string,     visited: boolean,     onclick: () => void,   };    state: {     display: 'static' | 'hover' | 'active';   };    static defaultprops = {     visited: false,   };    constructor(props) {     super(props);     this.state = {       display: 'static',     };   }    render() {     let classname = 'button ' + this.state.display;     if (this.props.visited) {       //...     }      return (/*...*/);   } } 

the thing can't flow types can proptypes defining custom validators (e.g. check prop valid email).

i have more flow react examples on github, didn't test them flow v0.22 yet, v0.21. might need minor adjustments.


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -