简体   繁体   English

尝试嵌套React组件

[英]Trying to nest React components

I'm new to React.js and am trying to use it to build myself a website. 我是React.js的新手,正在尝试使用它来构建自己的网站。 What I'm trying to do is to nest a child component within a parent component; 我要做的是将子组件嵌套在父组件中; the parent component should be rendered in the main page with the child component nested inside. 父组件应在主页中呈现,子组件嵌套在内部。 I've included an example below of my attempt. 我在下面提供了一个示例。

My approach thus far has been to 到目前为止,我的方法是

  1. Create the child component in 'childComponent.js' 在'childComponent.js'中创建子组件
  2. Export that child component as 'childComponent' 将该子组件导出为“ childComponent”
  3. Import 'childComponent' to parent component 将“ childComponent”导入父组件
  4. Render 'childComponent' in that parent component 在该父组件中渲染“ childComponent”
  5. Export that parent component as 'parentComponent' 将该父组件导出为“ parentComponent”
  6. Import 'parentComponent' to 'index.js' 将'parentComponent'导入到'index.js'
  7. Render 'parentComponent' in 'index.js' 在'index.js'中渲染'parentComponent'

The problem here is that 'childComponent' is never visible in my React app. 这里的问题是'childComponent'在我的React应用程序中永远不可见。 Am I taking the wrong approach here? 我在这里采用错误的方法吗? Is there a fundamental mechanic that I'm not understanding? 有我不了解的基本机制吗?

Thanks in advance!! 提前致谢!!

-- -

The child component (childComponent.js): 子组件(childComponent.js):

import React, { Component } from 'react';
import './childComponent.css';

class childComponent extends Component {
  render() {
    return (
      <div className="child-component">
        <span>Hello World</span>
      </div>
    );
  }
}

export default childComponent

childComponent.css: childComponent.css:

.child-component {
  width: 100vw;
  height: 300pt;
  background-color: #F4F4F4;
  display: flex block;
  justify-content: center;
  align-items: center;
  align-content: center;
}

The parent component (parentComponent.js): 父组件(parentComponent.js):

import React, { Component } from 'react';
import childComponent from './childComponent.js';
import './parentComponent.css';

class parentComponent extends Component {
  render() {
    return (
      <div className="parent-component">
        <childComponent></childComponent>
      </div>
    );
  }
}

export default parentComponent;

The main page (index.js): 主页(index.js):

import React from 'react';
import ReactDOM from 'react-dom';
import parentComponent from './parentComponent';
import './index.css';

ReactDOM.render(
  <parentComponent/>,
  document.getElementById('root')
);

Your first, and maybe only, issue is that react components must start with a capital letter. 您的第一个(也是唯一一个)问题是,react组件必须以大写字母开头。

Capitalized types indicate that the JSX tag is referring to a React component. 大写的类型表示JSX标签引用了React组件。 These tags get compiled into a direct reference to the named variable, so if you use the JSX expression, Foo must be in scope. 这些标记被编译成对命名变量的直接引用,因此,如果您使用JSX表达式,则Foo必须在范围内。

From: https://facebook.github.io/react/docs/jsx-in-depth.html#html-tags-vs.-react-components 来自: https : //facebook.github.io/react/docs/jsx-in-depth.html#html-tags-vs.-react-components

The comment advising removal of CSS and moving everything into one file is also good advice for debugging and the early stages of a React app. 建议删除CSS并将所有内容移动到一个文件中的注释对于调试和React应用程序的早期阶段也是一个很好的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM