1、组件,从概念上类似于 JavaScript 函数。它接受任意的入参(即 “props”),并返回用于描述页面展示内容的 React 元素。即他返回的是React元素,是可以放到JSX语法中使用的元素。
2、组件类型,根据书写方式分为函数组件和class组件:
// 函数式组件 function Welcome(props) { return <h1>Hello, {props.name}---{props.title}</h1>; } // class类组件 class WeGo extends React.Component { render() { return <h1>Hello, {this.props.name}</h1>; } }
3、组件嵌套,即组件中调用组件
// 嵌套组件 function App() { return ( <div> <Welcome name="Sara" title="子组件1"/> <Welcome name="Cahal" title="子组件1"/> <Welcome name="Edite" title="子组件1" /> </div> ); }
4、关于props
当 React 元素为用户自定义组件时,它会将 JSX 所接收的所有的属性(attributes)转换为单个对象传递给组件,这个对象被称之为 “props”。即自定义组件接收的参数props
并且,组件的Props 的只读性。所有 React 组件都必须像纯函数一样保护它们的 props 不被更改。 需要修改的时候这就用到了状态组件,拥有自己的state,以后会讲到。
5、关于组件规则
组件名称必须以大写字母开头。React 会将以小写字母开头的组件视为原生 DOM 标签