【问题标题】:why returning an **element** from const outcomes as an error?为什么从 const 结果返回 **element** 作为错误?
【发布时间】:2019-03-01 11:51:29
【问题描述】:

我返回的错误是:order is undefined来自浏览器中的控制台

当我将 order 替换为 index 甚至是积分 (5) 之类的东西时,它工作得很好。 order 元素可能有什么问题?

提前致谢!

render() {
const { classes, index, color, order, onClick } = this.props;
return (
  <div
    className={ClassNames({
      [classes.general]: true,
      [classes.borderBlack]: true,
      position:'relative'

    })}
    style={{ backgroundColor: color }}
    onMouseOver={this.mouseOver.bind(this)}
    onMouseLeave={this.mouseOut.bind(this)}
    //----Makes markers clickable
     onClick={onClick}
  >
    {index}
    {this.state.hover ? <div style={{
      color: 'white',
      position:'absolute',
     fontSize:30,
      backgroundColor: 'black'
       }}>{order}</div> : null}
  </div>
);
}

【问题讨论】:

  • 你有控制台记录 this.props 吗?
  • What could possibly be wrong with the order element? props 中没有定义。
  • 如果 this.props 中存在订单,那么它应该被破坏。通过 console.log 检查 this.props

标签: javascript reactjs element


【解决方案1】:

如果order 不在您的props 中,那么您将收到该消息。您可以在课堂上将其设置为defaultProp,或在propTypes 中将其标记为Required

<MyComponent {...otherProps} order={myOrder} /> // has to be there

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-05
    • 2016-05-28
    • 2015-10-22
    • 1970-01-01
    • 2019-05-22
    • 1970-01-01
    相关资源
    最近更新 更多