面试题
1 react 有哪几种定义组件的方式?
- 函数组件
// 接受唯一带有数据的 props 对象并返回一个 react 元素
// 这类组件称为“函数组件”
function Welcome(props) {
return <h1>hello, {props.name}</h1>
}
- es5 原生方式的 React.createClass 定义组件
var Welcome = React.createClass({
defaultProps: {
name: '',
},
render: function() {
return return <h1>hello, {this.props.name}</h1>
}
})
- class 组件
// 用 ES6 的 class 来定义组件
class Welcome extends React.Component {
render() {
return <h1>hello, {this.props.name}</h1>
}
}