【问题标题】:Unable to load second component in first component in ReactJS无法在 ReactJS 的第一个组件中加载第二个组件
【发布时间】:2017-09-06 16:22:02
【问题描述】:

为什么我的第二个组件没有加载到第一个组件中?代码如下:

var ComponentOne = React.createClass({
    render: function(){
      return(
        <div>
          <h1>ComponentOne</h1>
          <componentTwo />
        </div>
      )
    },
  });

  var componentTwo = React.createClass({
    render: function(){
      return(
        <div>
          <h2> componentTwo </h2>
        </div>
      )
    }
  })

这里我通过&lt;componentTwo /&gt;在第一个组件中添加了第二个组件,但仍然没有显示。

这里是a Plunker

【问题讨论】:

标签: reactjs components


【解决方案1】:

只要用大写字母开头你的component2就可以了

类似

var ComponentOne = React.createClass({
    render: function(){
      return(
        <div>
          <h1>ComponentOne</h1>
          <ComponentTwo />
        </div>
      )
    },
  });

  var ComponentTwo = React.createClass({
    render: function(){
      return(
        <div>
          <h2> componentTwo </h2>
        </div>
      )
    }
  })

为什么是大写字母?解释可以在这里找到https://stackoverflow.com/a/30373505/2551236

希望这会有所帮助!

祝你好运!

【讨论】:

  • 我能知道为什么组件名称要大写吗?
  • @RamanaUday 自定义组件必须以大写字母开头,因为内置组件(divh1p 等)使用小写。有关详细信息,请参阅the documentation
  • @RamanaUday 如果这个答案对您有帮助,请接受它,以便其他面临相同问题的人可以解决问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-17
  • 2016-08-21
  • 1970-01-01
  • 2020-07-25
  • 1970-01-01
相关资源
最近更新 更多