reactjs - PropTypes in react es6 doesn't work -
i created component , want validate 1 of property. seems validation doesn't work. when access component, got below property undefined error:
cannot read property 'map' of undefined
below component definition.
import react, {component, proptypes} 'react' import carousel 'nuka-carousel' export default class diagnosticschedule extends component { render() { let {style, diagnostics, afterslide, beforeslide} = this.props // if(diagnostics === undefined){ // return (<div/>) // } return ( <div style={style}> <carousel style={style} decorators={null} afterslide={afterslide} beforeslide={beforeslide}> { diagnostics.map(diagnostic => { return ( <div style={styles.message}> {diagnostic.name} </div> ) }) } </carousel> </div> ) } } diagnosticschedule.proptypes = { diagnostics: proptypes.array.isrequired, }; const styles = { carousel:{ }, message: { fontsize: 'xx-large', flex: 1, backgroundcolor: 'red', alignself: 'center', lineheight: '5', zindex: 100, width: '100%', textalign: 'center', } }
i think below code used validate component's property.
diagnosticschedule.proptypes = { diagnostics: proptypes.array.isrequired, };
but still complains the diagnostics undefined object.
do miss or misunderstand proptypes validation? expected behavior of proptypes?
thanks
the error happens because diagnostics
prop doesn't default value. if don't pass prop, set undefined
. fix situation, can set defaultprops
this:
diagnosticschedule.defaultprops = { diagnostics: [] };
Comments
Post a Comment