简体   繁体   English

onClick事件的调度动作

[英]Dispatch action for onClick event

In react component I have made a function as 在react组件中,我做了一个功能

 handleClick(event, key, value) {
    event.preventDefault()
    this.setState({
      query:"",
      key:key,
      value:value
    });
    this.props.onClick(this.state);
    console.log("key...." + key   );
    console.log("val...." + value);
  }

this should be called when I am clicking a link 单击链接时应调用此名称

onClick={(e) => this.handleClick(e, {key}, {el}) }

Also using connect I am dispatching action as follows 也使用连接我调度动作如下

export const mapDispatchToProps = (dispatch) => ({

  onClick: (query, key, value) => dispatch(onSearch(query,key, value)


  )
});

But on search method key and value are coming as undefined.But in handleClick function I am getting 但是在搜索方法中,键和值都不确定。但是在handleClick函数中,我得到了

key....[object Object]
val....[object Object]

key and value will be undefined. 键和值将是不确定的。 You need to call props method on callback of setState because you will not get updated state value just after calling setState. 您需要在setState的回调函数上调用props方法,因为在调用setState之后,您将不会获得更新的状态值。

handleClick(event, key, value) {
        event.preventDefault()
        this.setState({
          query:"",
          key:key,
          value:value
        },()=>{

           this.props.onClick(this.state);
        console.log("key...." + key   );
        console.log("val...." + value);
    });

      }

The way your calling the 'onClick' function is wrong, right now you are only passing one argument and not three. 您调用“ onClick”函数的方式是错误的,现在您只传递了一个参数,而不是三个。

It should be called like this 应该这样称呼

this.props.onClick(query, key, value);

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

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