猿问

react 渲染value时 onchange被多次触发

 <input type="text" className="numInput" onChange={(event)=>{

                                 console.log(event.target.value);

                                changeCount(cartId,event.target.value)

                            }} value={defCount}/>

使用框架为react+redux, 我再改变输入框值时,会触发onchange事件,但是在react渲染对value重新赋值时,会再次触发onchange,我想只在改变输入框值时触发一次,请问大神如何解决呢?

翻阅古今
浏览 885回答 1
1回答

收到一只叮咚

把value换成defaultValue&nbsp;<input type="text" className="numInput" onChange={(event)=>{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;console.log(event.target.value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; changeCount(cartId,event.target.value)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }} defaultValue={defCount}/>
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答