【问题标题】:Passing a function with parameters through props on reactjs通过 reactjs 上的 props 传递带参数的函数
【发布时间】:2017-05-13 04:14:03
【问题描述】:

我有一个从父级一直到组件层次结构中子级的函数。通常这不会有太大问题,但我需要从孩子那里接收一个参数。 我目前收到此错误消息:

Uncaught (in promise) TypeError: this.props.myFunction is not a function.

这是我正在做的示例代码:

class SomeComponent extends Component{

    constructor(props){
        super(props);
        //does whatever stuff        
        this.myFunction = this.myFunction.bind(this);

    }

    //(only applicable to raw and normal forms)
    myFunction(param){
        console.log('do something: ', param);
    }

    render(){
     return (<div><ChildComponent1 myFunction={()=>this.myFunction()}/></div>)
    }
}

class ChildComponent1{
      render(){
  return (<div><ChildComponent2 myFunction={()=>this.props.myFunction()}/></div>)
    }
}

class ChildComponent2{
      render(){
  return (<Button onClick={()=>this.props.myFunction(param)}>SomeButton</Button>)
    }
}

总结一下:我将 myFunction 作为 prop 从 SomeComponent 一直传递到 ChildComponent2,我希望在单击按钮时调用它并从 ChildComponent2 传递参数.

谢谢!

【问题讨论】:

  • 实际的问题可能是您的 childComponent1 并没有真正转发任何可能被调用的参数,也许您应该重构它,使您转发的函数是'最终不是箭头函数

标签: javascript reactjs


【解决方案1】:

我不明白您为什么会收到该错误,但您应该这样做 myFunction={this.myFunction}myFunction={this.props.myFunction}

class SomeComponent extends Component{

    constructor(props){
        super(props);
        //does whatever stuff        
        this.myFunction = this.myFunction.bind(this);

    }

    //(only applicable to raw and normal forms)
    myFunction(param){
        console.log('do something: ', param);
    }

    render(){
     return (<div><ChildComponent1 myFunction={this.myFunction}/></div>)
    }
}

class ChildComponent1{
      render(){
  return (<div><ChildComponent2 myFunction={this.props.myFunction}/></div>)
    }
}

class ChildComponent2{
      render(){
  return (<Button onClick={()=>this.props.myFunction(param)}>SomeButton</Button>)
    }
}

将函数调用包装在另一个(箭头)函数中是不必要的,并且不会正确转发参数(因为所有中间箭头函数都不接受参数)。

【讨论】:

  • 这适用于变量吗?如果它下降了两个级别怎么办?您也将它们设为子类,但如果我们谈论的是导出/导入的组件怎么办?
  • 我正在尝试您的解决方案,但我有一个 Main.js 和一个 Detail.js 文件,我试图在 Main 中声明 myFunction 并像这样使用它:this.props.myFunction(param ),在我的 detail.js 中。但我对 this.props.myFunction 没有定义。
  • React 功能组件可以吗?
【解决方案2】:

另一种 IMO 更干净的方法是这样的:

class SomeComponent extends Component{
    myFunction = param => {
        console.log('do something: ', param);
    }

    render(){
     return (
       <div>
         <ChildComponent1 onClick={this.myFunction}/>
       </div>)
    }
}

class ChildComponent1{
      render(){
        return (<div><ChildComponent2 onClick={this.props.onClick}/></div>)
      }
}

class ChildComponent2{
      render(){
        const { onClick } = this.props // destructure
        return (<Button onClick={()=>onClick(param)}>SomeButton</Button>)
      }
}

【讨论】:

  • 似乎对 'myFunction = param => {' 做了一些更新,但我找不到新版本。有任何想法吗?不再工作。
  • 应该在 ChildComponent2 中定义参数,例如var param ='ParamValue' ?
  • 是的,这是正确的做法。答案是按照提出问题的方式编写的 - 因此它假设您声明了您的参数(如您建议的那样)或在实施时直接与参数交换它。如果您直接使用它,我的示例将会中断。
  • 因此,如果您需要将参数传递给函数,请将表达式放在匿名函数体中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-26
  • 1970-01-01
  • 2018-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-05
相关资源
最近更新 更多