首页 > 解决方案 > React this.props 在状态事件中未定义?

问题描述

我正在尝试在状态下使用值props,但我收到未定义的错误,并且值也未显示在控制台日志上。这是我所做的代码:dataPointSelectionapexchartthis.props

constructor(props) {
    super(props);
    const { t, too, fromm } = this.props;
    console.log("Upper" + this.props.fromm);
    this.state = {
      ...props,
      options: {
        chart: {
          id: "Speeding",
          events: {
            dataPointSelection: function(event, chartContext, opts) {
              switch (opts.w.config.xaxis.categories[opts.dataPointIndex]) {
                case "0-10":
                  window.open(
                    window.location.origin +
                      `/overspeeding/10/0/${
                        this.props.too ? `${this.props.too}` : `${cc}`
                      }/${
                        this.props.fromm ? `${this.props.fromm}` : "2016-01-04"
                      }` // in this line i am getting error
                  );

如何解决此错误和事件中的用户道具值?

标签: javascriptreactjsdom-eventsreact-propsapexcharts

解决方案


您通常不需要在构造函数中的道具后面使用它。只用道具试试


推荐阅读