React Native conditionally accepting props -




i making autocomplete component live search. these props:

                    <autocompleteinput                         ref={autocomplete.tag}                         tag={autocomplete.tag}                         type={autocomplete.type}                         title={autocomplete.title}                         required={autocomplete.required}                         photorequired={autocomplete.photorequired}                         defaultvalue={autocomplete.defaultvalue}                         options={autocomplete.options}                         titlekey={autocomplete.titlekey}                         valuekey={autocomplete.valuekey}                         singleselection={false}                         maxsuggestionnumber={50}                         minimumcharacternumber={-2}                      /> 

singleselection, maxsuggestionnumber , minimumcharacternumber optional props. want give default values them , should check prop values. example; default value of minimumcharacternumber should '0', if dont use prop, '0' value should passed component. but, if value given lower 0, component should use default value '0'. tried function public static defaultprops() { ...... } , confused , couldn't handle conditions. ordinary technique ? solution appriciated.

edit: solved issue, , want share.

public static defaultprops = {     minimumcharacternumber: 0,     maxsuggestionnumber: 50,     singleselection: false, }  constructor(props: acmodalprops) {     super(props)     this.state = {         maxsuggestionnumber:         this.props.maxsuggestionnumber && this.props.maxsuggestionnumber > 0             ? this.props.maxsuggestionnumber             : acmodal.defaultprops.maxsuggestionnumber,          minimumcharacternumber:         this.props.minimumcharacternumber && this.props.minimumcharacternumber > 0             ? this.props.minimumcharacternumber             : acmodal.defaultprops.minimumcharacternumber,         singleselection: this.props.singleselection             ? this.props.singleselection             : acmodal.defaultprops.singleselection,                 .               .               . 

i created public static defaultprops, put default values. passed these attributes state, check values in state. used values state. if use typescript, shouldnt forget write these attributes' names in state interface proptypes.

default props assigned after class or function definition:

class autocompleteinput extends component {     ... }  autocompleteinput.defaultprops = {     title: 'hello world' } 

see react documentation on prop types more information.





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -