简体   繁体   English

React.js onChange = {value => {}}多个值

[英]React.js onChange={value => {}} multiple values

I am trying to abstract some source code, I know other languages but little about React.js. 我正在尝试抽象一些源代码,我知道其他语言,但对React.js知之甚少。

I was hoping for a little more information as to why only my second onchange is being hit 我希望能获得更多信息,以了解为什么只有第二次onchange被击中

I was hoping for some help on how to update "value" & "drag_value" onchange 我希望在onchange时更新“ value”和“ drag_value”时获得帮助

onChange={value => {                        
    this.setState({value});
    console.log("value value")
    console.log(value, drag_value)

    if (updatemode === 'drag') {
        setProps({value});
    }
    if (updatemode === 'hybrid') {
        setProps({drag_value});
    }
}}
onChange = {drag_value => {
    this.setState({drag_value});
    console.log("drag value")
    console.log(value, drag_value)

    if (updatemode === 'drag') {
        setProps({value});
    }
    if (updatemode === 'hybrid') {
        setProps({drag_value});
    }
}}

You are decrlairing 2 function under the same name and basically overriding the first one. 您正在使用相同的名称来破坏2函数,并且基本上覆盖了第一个函数。

it will be better to use names such as "handleChange" and "handleDragChange" or something like that , just so it will be different . 最好使用“ handleChange”和“ handleDragChange”之类的名称,或者那样的名称,那样会有所不同。

you are overriding your 1st handleChange with your 2nd handleChange . 要覆盖你的第一handleChange与第二handleChange So if u reverse their order then your handleChange with value will get hit as in javascript the final most declaration of any function is considered. 因此,如果您颠倒其顺序,则您的带有valuehandleChange将被命中,因为在javascript中,将考虑所有函数的最终声明。 To avoid this situation, give your functions different names and watch them work like a charm. 为避免这种情况,请给您的函数起不同的名称,并注意它们的作用。 Hope this helps!! 希望这可以帮助!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM