首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TypeError:无法读取null的“opacity”属性

TypeError:无法读取null的“opacity”属性
EN

Stack Overflow用户
提问于 2018-07-28 19:47:45
回答 1查看 511关注 0票数 2

当鼠标悬停在React recharts中的图例上时,我正在尝试实现不透明度更改:https://jsfiddle.net/alidingling/1p40zzfe/

但是,我总是收到错误: TypeError: Cannot read property 'opacity‘of null。有什么原因吗?导致问题的是具有行"OnMouseEnter“和"OnMouseLeave”的图例组件。

谢谢

代码语言:javascript
复制
class EducationPageRouter extends React.Component{
  getInitialState() {
    return {
        opacity: {
        car: 1,
        phone: 1,
      },
    };
  }

  handleMouseEnter(o) {
    const { dataKey } = o;
    const { opacity } = this.state;

    this.setState({
        opacity: { ...opacity, [dataKey]: 0.5 },
    });
  }

  handleMouseLeave(o) {
    const { dataKey } = o;
    const { opacity } = this.state;

    this.setState({
        opacity: { ...opacity, [dataKey]: 1 },
    });
  }

  render() {
    const { opacity } = this.state;

    return (

      <Card>
        <CardContent>
          <Typography variant="title" color="primary" gutterBottom="true">
            Sales Information Display
          </Typography>
          <Typography variant="header1" color="primary" gutterBottom="true">
            Line Graph - Number of Sales to Date Time
          </Typography>
          <Button variant="raised" color="primary">
            Switch
          </Button>

          <ResponsiveContainer width="95%" aspect={9.5/3.8}>
            <LineChart
              data={formattedSales}
              margin={{ top:5, right:10, bottom:5, left:20 }}
            >
              <XAxis
                dataKey="date"
                padding={{ left:0, right:50 }}
                style={{ fontFamily: "Roboto, sans-serif" }}
                tick={{ fontSize: "12.5px"}}
              />
              <YAxis
                dataKey="car"
              />
              <CartesianGrid
                stroke="#f5f5f5"
                strokeDasharray="2.5 2.5"
              />
              <Tooltip
                wrapperStyle={{ fontFamily: "Roboto, sans-serif" }}
              />

              <Legend
                onMouseEnter={this.handleMouseEnter} 
                onMouseLeave={this.handleMouseLeave}
                wrapperStyle={{ fontFamily: "Roboto, sans-serif" }}
                verticalAlign="bottom"
                height={40}
              />

              <Line
                type="monotone"
                dataKey="car"
                stroke="#4169e1"
              />
              <Line
                type="monotone"
                dataKey="phone"
                stroke="#fa8072"
              />
            </LineChart>
          </ResponsiveContainer>
        </CardContent>
      </Card>
    )
  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-28 20:05:49

getInitialState只能与createReactClass一起使用,因此您的状态将为null。您可以改为在构造函数中设置初始状态,也可以使用class属性。

您的事件处理程序handleMouseEnterhandleMouseLeave未绑定,因此this将不是您所期望的。一种解决方法是将它们绑定到构造函数中的this,或者使它们成为属性初始化的箭头函数。

代码语言:javascript
复制
class EducationPageRouter extends React.Component {
  state = {
    opacity: {
      car: 1,
      phone: 1,
    }
  };

  handleMouseEnter = o => {
    const { dataKey } = o;
    const { opacity } = this.state;

    this.setState({
      opacity: { ...opacity, [dataKey]: 0.5 }
    });
  };

  handleMouseLeave = o => {
    const { dataKey } = o;
    const { opacity } = this.state;

    this.setState({
      opacity: { ...opacity, [dataKey]: 1 }
    });
  };

  // ...
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51571081

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档