Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 如何调用react中的道具箭头函数_Reactjs_Arrow Functions - Fatal编程技术网

Reactjs 如何调用react中的道具箭头函数

Reactjs 如何调用react中的道具箭头函数,reactjs,arrow-functions,Reactjs,Arrow Functions,我是一个初学者。 我有一个带有UpdateUsersprops函数的组件,它在主组件中调用: UpdateUsers = () => {...} render(){ return( <User UpdateUsers = {this.UpdateUsers} /> )} addUser = async () => { //do some other things await //code for adding user this.

我是一个初学者。 我有一个带有
UpdateUsers
props函数的组件,它在主组件中调用:

UpdateUsers = () => {...}
 render(){
   return(
     <User UpdateUsers = {this.UpdateUsers} />
  )}
addUser = async () => {
   //do some other things
   await //code for adding user
   this.props.UpdateUsers();
}

render(){
   return(
     //do some other things
     <Button onClick={this.addUser}>ADD<Button/>
  )}
主要组成部分:

UpdateUsers = () => {...}
 render(){
   return(
     <User UpdateUsers = {this.UpdateUsers} />
  )}
addUser = async () => {
   //do some other things
   await //code for adding user
   this.props.UpdateUsers();
}

render(){
   return(
     //do some other things
     <Button onClick={this.addUser}>ADD<Button/>
  )}

this.props.UpdateUsers()addUser
中调用的code>无效。如果我在按钮
OnCkick
中调用它,它会工作。但是在
ddUser
中调用它并不能正常工作


如何在
addUser
函数中调用
this.props.UpdateUsers

我不确定问题出在哪里,因为我看不到
主组件和
用户组件的整个代码。如果你能多告诉我一些你的问题,那会很有帮助的

如果在您单击某个按钮后没有调用
UpdateUsers
,下面的代码会解决它,但是如果调用了它并且没有达到您预期的效果,那么我需要更多地了解您的问题

class User extends Component {
  addUser = () => {
    this.props.addUser();
  }
  
  render() {
    return (
      <button onClick={this.addUser}>
        Add user
      </button>
    );
  }
}

class Main extends Component {
  addUser = () => {
    // do something here 
  }

  render() {
    return <User addUser={this.addUser} />;
  }
}
类用户扩展组件{
addUser=()=>{
this.props.addUser();
}
render(){
返回(
添加用户
);
}
}
类主扩展组件{
addUser=()=>{
//在这里做点什么
}
render(){
返回;
}
}

我不确定问题出在哪里,因为我看不到
用户
组件的全部代码。如果你能多告诉我一些你的问题,那会很有帮助的

如果在您单击某个按钮后没有调用
UpdateUsers
,下面的代码会解决它,但是如果调用了它并且没有达到您预期的效果,那么我需要更多地了解您的问题

class User extends Component {
  addUser = () => {
    this.props.addUser();
  }
  
  render() {
    return (
      <button onClick={this.addUser}>
        Add user
      </button>
    );
  }
}

class Main extends Component {
  addUser = () => {
    // do something here 
  }

  render() {
    return <User addUser={this.addUser} />;
  }
}
类用户扩展组件{
addUser=()=>{
this.props.addUser();
}
render(){
返回(
添加用户
);
}
}
类主扩展组件{
addUser=()=>{
//在这里做点什么
}
render(){
返回;
}
}
看看这个解决方案;
https://stackoverflow.com/questions/63302124/call-a-method-passed-as-a-prop-in-react
当您将方法作为道具发送时,可以使用.bind;
https://stackoverflow.com/questions/63302124/call-a-method-passed-as-a-prop-in-react

当您将方法作为道具发送时,可以使用.bind。

我意识到有些函数需要时间才能运行

在这种情况下,在
UpdateUsers
之前添加用户是一项耗时的功能。
UpdateUsers
无法在列表中显示新用户(我添加的),并显示在添加另一个用户后,这是时间问题

我们可以通过如下定义
async
函数来解决此问题:

在用户组件中:

UpdateUsers = () => {...}
 render(){
   return(
     <User UpdateUsers = {this.UpdateUsers} />
  )}
addUser = async () => {
   //do some other things
   await //code for adding user
   this.props.UpdateUsers();
}

render(){
   return(
     //do some other things
     <Button onClick={this.addUser}>ADD<Button/>
  )}
addUser=async()=>{
//做一些其他的事情
等待//用于添加用户的代码
this.props.UpdateUsers();
}
render(){
返回(
//做一些其他的事情
添加
)}

我意识到有些函数需要时间运行

在这种情况下,在
UpdateUsers
之前添加用户是一项耗时的功能。
UpdateUsers
无法在列表中显示新用户(我添加的),并显示在添加另一个用户后,这是时间问题

我们可以通过如下定义
async
函数来解决此问题:

在用户组件中:

UpdateUsers = () => {...}
 render(){
   return(
     <User UpdateUsers = {this.UpdateUsers} />
  )}
addUser = async () => {
   //do some other things
   await //code for adding user
   this.props.UpdateUsers();
}

render(){
   return(
     //do some other things
     <Button onClick={this.addUser}>ADD<Button/>
  )}
addUser=async()=>{
//做一些其他的事情
等待//用于添加用户的代码
this.props.UpdateUsers();
}
render(){
返回(
//做一些其他的事情
添加
)}

您当前的代码有什么问题吗?
this.props.UpdateUsers()addUser
中调用的code>不起作用。如果我在按钮
OnCkick
中调用它,它会工作。但是在
addUser
中调用它并不能正确地工作您是否调用了
super(props)在用户组件的构造函数中?请共享整个用户组件代码以及错误(如果存在)您所说的“无法正常工作”是什么意思?如果有错误,请分享准确的详细信息。您当前的代码有什么问题吗?
this.props.UpdateUsers()addUser
中调用的code>不起作用。如果我在按钮
OnCkick
中调用它,它会工作。但是在
addUser
中调用它并不能正确地工作您是否调用了
super(props)在用户组件的构造函数中?请共享整个用户组件代码以及错误(如果存在)您所说的“无法正常工作”是什么意思?请分享准确的错误详细信息(如有)。