【React】React.Component小结
2016-12-13 22:41
169 查看
React.Component 是一个抽象的Class,通常继承该类来构建自定义的Component。 Component可以将U分离成独立的碎片,有点类似于JavaScript的function,它接受一个任意的输入(props)并返回一个React element描述屏幕中的内容。
有两种方法构建Components
注意: Component必须返回单个根元素,因而要用
将包住。
必须包含render()方法
constructor() // 构造函数
componentWillMount()
render()
componentDidMount()
2 Updating
componentWillReceiveProps()
shouldComponentUpdate()
componentWillUpdate()
render()
componentDidUpdate()
3 Unmounting
-componentWillUnmount()
每个Component中有
setState()
通过this.setState({value: dddd}) 更新
-displayName
string用来在调试中显示信息
-propTypes
state
存储一些数据信息,如果不在render()中使用,则最好不要放在state中。
有两种方法构建Components
1 JavaScript函数
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') );
注意: Component必须返回单个根元素,因而要用
将包住。
2 利用React.Component 创建
class Greeting extends React.Component { constructor(props){ super(props); this.state = { color: props.initialColor }; } render() { return <h1>Hello, {this.props.name}</h1>; } }
必须包含render()方法
Component 生命周期
1 Mounting (挂载)constructor() // 构造函数
componentWillMount()
render()
componentDidMount()
2 Updating
componentWillReceiveProps()
shouldComponentUpdate()
componentWillUpdate()
render()
componentDidUpdate()
3 Unmounting
-componentWillUnmount()
每个Component中有
setState()
通过this.setState({value: dddd}) 更新
Class Properties
-defaultPropsclass CustomButton extends React.Component { // ... } CustomButton.defaultProps = { color: 'blue' };
-displayName
string用来在调试中显示信息
-propTypes
class CustomButton extends React.Component { // ... } CustomButton.propTypes = { color: React.PropTypes.string };
Instance Properties
props<Greeting initialColor='blue' />
state
存储一些数据信息,如果不在render()中使用,则最好不要放在state中。
相关文章推荐
- 1. React介绍 React开发环境搭建 React第一个程序
- react-native播放视频组件 react-native-video的用法
- React:组件API
- 【React Native实战教程】GitHub Trending API数据的获取
- React-Native 错误集锦
- React Echarts 拖拽<github持续更新中>
- Qzone React Native改造
- react-native调用Android原生模块
- 【ReactJS】React项目构建的几种方式
- MacOS Sierra下运行react-native start出现Error watching file for changes: EMFILE问题解决
- React-Native环境配置及调试工具
- [译文&摘抄]在 React & Redux 中使用 AJAX 轮询
- React Native常用组件Image使用
- mac搭建react-native环境
- 搭建一个react项目
- 基于 React-Native 的高仿「ONE·一个」开源项目
- react-router JS 控制路由跳转实例
- react---启动本地项目,并更改端口
- React和Redux的连接react-redux【转载】
- React学习-- React源码(5)diff算法