facebook - this.state.text.split() is undefined error when using onSubmitEditing -
according facebook's official documentation:
onchangetext
takes function called every time text changes
and
onsubmitediting
takes function called when text submitted
i'm following official facebook tutorial on text input handling, code:
export default class pizzatranslator extends component { constructor(props) { super(props); this.state = {text: ''}; } render() { return ( <view style={{padding: 10}}> <textinput style={{height: 40}} placeholder="type here translate!" onchangetext={(text) => this.setstate({text})} /> <text style={{padding: 10, fontsize: 42}}> {this.state.text.split(' ').map((word) => word && '🍕').join(' ')} </text> </view> ); } }
takes sentence , change of words 🍕 emojis, if change onchangetext
onsubmitediting
, error saying this.state.text.split not function. (in 'this.state.text.split('')', 'this.state.text.split' undefined)
when submit text.
the documentation correct on point, both onchangetext
, onsubmitediting
take callback function executed. essential difference argument supplied callback function. onchangetext
gives new value string , onsubmitediting
executes callback function object first argument. can view result using
<textinput onchangetext={(text) => {this.setstate({text}); console.warn('text', text);}} onsubmitediting={(argument) => console.warn(object.keys(argument))} />
Comments
Post a Comment