【问题标题】:React: Pass function to a child not working反应:将功能传递给不工作的孩子
【发布时间】:2017-12-24 01:35:33
【问题描述】:

我无法将函数传递给 React 中的子级。我在stackoverflow 上阅读了多个线程,这些线程谈论将此类函数绑定到this 或使用arrow 函数,但仍然无法解决它。基本上我需要将名为datum 的函数传递给d3.select().datum()

class BarChart extends React.Component {
  constructor(props){
    super(props)
    this.createBarChart = this.createBarChart.bind(this)
  }

  componentDidMount() {
     this.createBarChart()
  }

  componentDidUpdate() {
     this.createBarChart()
  }

  createBarChart() {
    console.log("In createBarChart: " + this.props.datum);
    const node = this.node
    nv.addGraph(function() {
      var chart = nv.models.discreteBarChart()
        .x(function(d) { return d.label })
        .y(function(d) { return d.value })
        .staggerLabels(true)
        //.staggerLabels(historicalBarChart[0].values.length > 8)
        .showValues(true)
        .duration(250)
        ;
    d3.select(node)
        .datum(this.props.datum)
        .call(chart);
    nv.utils.windowResize(chart.update);
    return chart;
});
  }

  render() {
    return <svg ref={node => this.node = node}
      width={1000} height={500}>
    </svg>
  }

}

module.exports = BarChart; 

在上面的代码中 d3.select(node) .datum(this.props.datum) .call(chart); 原因

TypeError: this.props 未定义

我正在尝试通过以下方式将datum 函数传递给BarChart 组件:

import datum from './datum'

class App extends React.Component {
  render() {
    return (
      <DefaultLayout title={this.props.title}>
        <div>Hello {this.props.name}</div>
        <div className='App'>
          <BarChart datum = { datum.bind(this) }/>
        </div>
      </DefaultLayout>
    );
  }
}

module.exports = App;

我尝试过&lt;BarChart datum = { () =&gt; this.datum() }/&gt;,但没有运气。然后也在BarChart组件的constructor中绑定datum函数,类似于createBarChart函数:

 constructor(props){
     super(props)
     this.createBarChart = this.createBarChart.bind(this)
     this.props.datum = this.props.datum.bind(this)
 } 

我作为模块从datum.js 导入的datum 函数如下所示:

var datum = function datumFunc() {
   return  [
    {
      key: "Cumulative Return",
      values: [
      ...
      ]
    }
  ]
}

export default datum

任何建议将不胜感激。

【问题讨论】:

    标签: reactjs d3.js nvd3.js


    【解决方案1】:

    您传递给nv.addGraph 的匿名函数未绑定,因此调用该函数时this 超出范围。

    nv.addGraph(function() {
      var chart = nv.models.discreteBarChart()
        .x(function(d) { return d.label })
        .y(function(d) { return d.value })
        .staggerLabels(true)
        //.staggerLabels(historicalBarChart[0].values.length > 8)
        .showValues(true)
        .duration(250)
        ;
      d3.select(node)
        .datum(this.props.datum)
        .call(chart);
      nv.utils.windowResize(chart.update);
      return chart;
    }.bind(this));
    //^^^^^^^^^^ would fix it
    

    或者,您可以为该函数命名并将其绑定到构造函数中,就像您已经对 createBarChart 所做的那样。

    【讨论】:

    • 即使在最新版本的 ReactJS 中,构造函数已成为可选?
    • @HoCo_ 我不确定您具体指的是哪个更新,但在任何情况下this 用于异步上下文(例如此处的回调),都应绑定适当的值。我在这里考虑的唯一现代化是使用 () =&gt; { } 作为函数,这是实现与 function () { }.bind(this) 相同的功能的语法糖。
    • 我明白了,谢谢,我谈到了更新,我们现在可以直接在组件的预渲染空间中声明状态,而无需声明构造函数,今天早上我有一些错误要通过一个没有声明构造函数的函数,最后,我把这个函数直接放在了child中作为一种解决方法
    • @HoCo_ 在构造函数之外声明状态(在不依赖props的情况下)与需要将this绑定到回调方法无关。
    猜你喜欢
    • 1970-01-01
    • 2023-03-18
    • 2018-04-09
    • 1970-01-01
    • 2021-01-23
    • 1970-01-01
    • 2021-12-28
    • 2021-01-27
    • 2017-08-22
    相关资源
    最近更新 更多