函数式组件
实际上就是一个函数,传入props作为参数,返回一个react组件,这种就称之为函数式组件
function Welcome(props) {return <h1>Hello, {props.name}</h1>;}// 是一个字符串const element = <Welcome name="Sara" />;ReactDOM.render(element,document.getElementById('root'));
class组件
使用ES语法,通过继承React.componet来创建的类,称之为class组件
class Welcome extends React.Component {render() {return <h1>Hello, {this.props.name}</h1>;}}
组合组件
组件可以在其输入中返回引用其他组件,这种一般是组合组件
function Welcome(props) {return <h1>Hello, {props.name}</h1>;}function App() {return (<div><Welcome name="Sara" /><Welcome name="Cahal" /><Welcome name="Edite" /></div>);}ReactDOM.render(<App />,document.getElementById('root'));
注意
- 自定义组件必须是以大写字母开头,因为React默认会解析小写字母开头的标签为html标签
- 不能随意修改传入的props值
- state和props类似,但是state是私有的,完全受控于当前组件
