在反应中最令人困惑的不变违反

时间:2019-03-04 04:35:30

标签: javascript reactjs

请考虑以下内容:

import React from 'react';
import ReactDOM from 'react-dom';
import Game from './game';

const game = document.getElementById('game');

if (game !== null) {
  ReactDOM.render(
    <React.Strict>
      <Game />
    </React.Strict>,
    game
  );
}

Game的定义如下:

import React from 'react';

export default class Game extends React.Component {
  constructor(props) {
    super(props);

    this.apiUrl = window.location.protocol + '//' + window.location.host + '/';
  }

  render() {
    return (
      <div>


      </div>
    )
  }
}

我从中删除了所有内容,开始时没有太多内容,但是仍然可以在浏览器控制台中看到:

  

未捕获的不变违反:元素类型   无效:预期为字符串(对于内置组件)或   类/函数(用于复合组件),但得到:未定义。您   可能忘记了从定义的文件中导出组件,   否则您可能混淆了默认导入和命名导入。

最后,我检查了导出和导入的正确性。我在想什么,这应该不会使React.Strict失败。

1 个答案:

答案 0 :(得分:3)

根据文档here,应为<React.StrictMode>,而不是<React.Strict>