javascript - react-native onChangeText this.value is undefined -
i adding simple masking text input, date in dob field looks dd/mm/yyyy.
i have function handle that:
dateformat(text) { if (text.match(/^\d{2}$/) !== null) { this.value = text + '/'; } else if (text.match(/^\d{2}\/\d{2}$/) !== null) { this.value = text + '/'; } this.setstate({birthdate: this.value}); }
i call function with:
onchangetext={(text) => this.dateformat(text)}
the text passed correctly. however, this.value undefined , setting whatever, makes no difference.
what doing wrong?
i don't think need reference this.value, declare var value = text:
onchangetext(text){ if (text.match(/^\d{2}$/) !== null) { var value = text + '/'; this.setstate({birthdate: value}) } else if (text.match(/^\d{2}\/\d{2}$/) !== null) { var value = text + '/'; this.setstate({birthdate: value}); } else {this.setstate({birthdate:text})} }
i've set on rnplay here, , seems working fine:
https://rnplay.org/apps/o9a7xq
'use strict'; var react = require('react-native'); var { appregistry, stylesheet, text, view, textinput } = react; var sampleapp = react.createclass({ getinitialstate(){ return { birthdate: '' } }, onchangetext(text){ if (text.match(/^\d{2}$/) !== null) { var value = text + '/'; this.setstate({birthdate: value}) } else if (text.match(/^\d{2}\/\d{2}$/) !== null) { var value = text + '/'; this.setstate({birthdate: value}); } else {this.setstate({birthdate:text})} }, render: function() { return ( <view style={styles.container}> <textinput value={this.state.birthdate} onchangetext={ (text) => this.onchangetext(text) } style={styles.textinput} /> <text>{this.state.birthdate}</text> </view> ); } }); var styles = stylesheet.create({ container: { flex: 1, margintop:60 }, textinput: { height:70, backgroundcolor: '#ddd' } }); appregistry.registercomponent('sampleapp', () => sampleapp);
Comments
Post a Comment