这次给大家带来react输入数据并同步,react输入数据并同步的注意事项有哪些,下面就是实战案例,一起来看一下。
要求如下
输入框输入内容数据长度大于0,展示出预览信息
光标离开关闭预览信息
预览信息每隔4位插入一个特殊字符_,输入内容不变
限制长度为13位
只允许输入数字(0-9)
// zinput.jsimport react, { component} from 'react';import './zinput.css'// note: 获取焦点事件 原生onfocus 即可// note: 离开焦点事件 原生onblur即可// note: 输入框数据过滤 直接在change方法里进行过滤// note: 条件处理 通过不同条件返回不同节点做条件处理class zinput extends component { constructor(props) { super(props); this.state = { value: '', showbig: false, }; this.handlechange = this.handlechange.bind(this); this.inputonfocus = this.inputonfocus.bind(this); this.inputonblur = this.inputonblur.bind(this); } inputonfocus() { if (this.state.value.length > 0) { this.setstate({ showbig: true }) } } inputonblur() { this.setstate({ showbig: false }) if(this.props.chanegnumber){ this.props.chanegnumber(this.state.value) } } handlechange(event) { let val = event.target.value.substr(0, 13) .replace(/[^\d]/g, '') event.target.value = val this.setstate({ value: val, showbig: true, }); } /** * 根据字符串没隔len位插入一个下滑杠,返回处理后的字符串 * @method getstr * @author 朱阳星 * @datetime 2018-04-02t09:57:58+080 * @email zhuyangxing@foxmail.com * @param {string} str 待处理字符串 * @param {number} len 每隔位数插入下滑杠 * @return {string} 处理后的字符串 */ getstr(str, len) { let lenth = str.length let len1 = len - 1 let newstr = '' for (var i = 0; i < lenth; i++) { if (i % len === len1 && i > 0) { newstr += str.charat(i) + '_' } else { newstr += str.charat(i) } } if (newstr.length % (len + 1) === 0) { // 解决最后一位为补充项问题 newstr = newstr.substr(0, newstr.length - 1) } return newstr } render() { // note return 需要用圆括号包住并处理 // note 条件语句里没有节点也要用空字符串进行处理 否则sonalint会报错,页面也会报错 const showbig = this.state.showbig ? ( <p classname="big-show">{ this.getstr(this.state.value,4) }</p> ) : '' return ( <p classname="zinput"> <input classname="input" type = "text" onfocus={ this.inputonfocus } onblur={ this.inputonblur } value={ this.state.value } onchange={ this.handlechange }> </input> {showbig} </p> ) }}export default zinput; // don't forget to use export default!
<!-- zinput.css -->.zinput{ position: absolute; top:80px; left:40px; }.input { position: absolute; top: 0; left: 0;}.big-show { position: relative; top: -40px; font-size: 36px; line-height: 40px; background-color: red;}
功能虽然实现,但是肯定是作为某个节点的某个子组件使用的,父组件调用方法有两种
1.使用refs直接获取子组件state的值
constructor(props) { super(props); this.handerclick2 = this.handerclick2.bind(this);}handerclick2(){ // note 父组件通过refs获取子组件的state console.log(使用ref获取子组件的值,this.refs.zinput.state.value)}render() { return ( <p classname="app"> <zinput ref="zinput"></zinput> <input type="button" value="获取电话号码的值22" onclick={ this.handerclick2 }/> </p> );}
2.每次子组件焦点离开时调用父组件传过来的方法,修改父组件state值
constructor(props) { super(props); this.state = { phonenumber: '', }; this.handerclick = this.handerclick.bind(this); this.changephonenumber = this.changephonenumber.bind(this);}changephonenumber(number){ this.setstate({ phonenumber: number, })}handerclick(){ // note 根据react的思想是在子组件处理完某件事的时候调用父组件的方法修改父组件的state值 console.log(使用state获取值,this.state.phonenumber)}render() { return ( <p classname="app"> <zinput ref="zinput" chanegnumber={this.changephonenumber}></zinput> <input type="button" value="获取电话号码的值" onclick={ this.handerclick }/> </p> );}
相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!
推荐阅读:
vue+mint-ui在项目中如何使用
怎样使用vue做出单页应用前端路由
以上就是react输入数据并同步的详细内容。