【问题标题】:ReactJS error: make sure to pass up the same props that your component's constructor was passedReactJS 错误:确保传递与组件的构造函数相同的道具
【发布时间】:2018-09-18 19:00:06
【问题描述】:

我正在学习“清晰”的 ReactJS(即它不是 JSX)。这是我的简单代码:

(() => {
    class List extends React.Component{
        constructor({tag, attributes, items}){
            super({tag, attributes, items})
            this.tag = tag
            this.attributes = attributes
            this.items = items
        }
        render(){
            return React.createElement(this.props.tag, this.props.attributes, items.map(
                (n, i) => new React.createElement(ListItem, 
                    { attributes: { ...n.attributes, key: i }, value: n.value })))
        }
    }

    class ListItem extends React.Component{
        constructor({attributes, value}){
            super({attributes, value})
            this.attributes = attributes
            this.value = value
        }
        render(){
            return React.createElement("li", this.props.attributes, this.props.value)
        }
    }

    const items = [
        { attributes: null, value: "A" },
        { attributes: null, value: "B" },
        { attributes: null, value: "C" },
        { attributes: null, value: "D" },
        { attributes: null, value: "E" }
    ]

    const root = document.getElementById("root")

    ReactDOM.render(new React.createElement(List, { tag: "ul", attributes: null, items }), root)
})()

请注意:我在super 中放入了与组件构造函数中相同的参数。我也将key 用于li 元素。但我得到这样的错误:

警告:List(...): 在List 中调用 super() 时,请确保通过 与传递组件的构造函数相同的道具。

警告:数组或迭代器中的每个子元素都应该有一个唯一的“键” 支柱。

检查List的渲染方法。

在ListItem中(由List创建)

在列表中

警告:ListItem(...):在ListItem 中调用 super() 时,请确保 传递与组件的构造函数相同的道具 通过了。警告:ListItem(...):在ListItem 中调用 super() 时, 确保传递与组件的构造函数相同的道具 通过了。警告:ListItem(...):在调用 super() 时 ListItem,确保传递与您的组件相同的道具 构造函数被传递。警告:ListItem(...):调用 super() 时 在ListItem 中,确保传递与您的相同的道具 组件的构造函数已通过。警告:ListItem(...):当 在ListItem 中调用 super(),确保传递相同的道具 您的组件的构造函数已通过。

我做错了什么?

【问题讨论】:

    标签: javascript reactjs


    【解决方案1】:

    您需要使用单个变量,例如 props 作为 constructor 函数的参数。然后将其传递给super。下面的代码修复了所有警告。

    class List extends React.Component {
      constructor(props) {
        super(props);
        this.tag = props.tag;
        this.attributes = props.attributes;
        this.items = props.items;
      }
      render() {
        return React.createElement(
          this.props.tag,
          this.props.attributes,
          items.map(
            (n, i) =>
              new React.createElement(ListItem, {
                attributes: { ...n.attributes, key: i },
                value: n.value
              })
          )
        );
      }
    }
    
    class ListItem extends React.Component {
      constructor(props) {
        super(props);
        this.attributes = props.attributes;
        this.value = props.value;
      }
      render() {
        return React.createElement("li", this.props.attributes, this.props.value);
      }
    }
    
    const items = [
      { attributes: null, value: "A" },
      { attributes: null, value: "B" },
      { attributes: null, value: "C" },
      { attributes: null, value: "D" },
      { attributes: null, value: "E" }
    ];
    
    const root = document.getElementById("root");
    
    ReactDOM.render(
      new React.createElement(List, { tag: "ul", attributes: null, items }),
      root
    );
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
    	<div id="root"></div>

    Official doc 表示类组件应始终使用props 调用基本构造函数。

    当我看source:

    const hasMutatedProps = instance.props !== newProps;
    warningWithoutStack(
        instance.props === undefined || !hasMutatedProps,
          '%s(...): When calling super() in `%s`, make sure to pass ' +
          "up the same props that your component's constructor was passed.",
          name,
          name,
      );
    

    react 检查它在constructor 函数中收到的props 对象是否与您使用super() 传递给基类的对象相同。在您的代码中,当然不一样。例如,您传递给super({attributes, value}) 的内容完全是一个不同的对象。因为在 JS 中:

    {a: 1, b: 2} === {a:1, b:2} // false
    {a: 1, b: 2} !== {a:1, b:2} // true
    

    【讨论】:

      【解决方案2】:

      你可以在渲染方法中使用解构

      class List extends React.Component{
          constructor(props){
              super(props).....
      
          render(){
              const {tag, attributes, items} = this.props
              return React.....
      

      【讨论】:

        猜你喜欢
        • 2018-05-07
        • 1970-01-01
        • 2021-01-20
        • 2019-08-06
        • 1970-01-01
        • 2017-03-01
        • 2018-08-09
        • 2019-03-08
        • 1970-01-01
        相关资源
        最近更新 更多