如何将ReactJS组件的state和props挂载阶段的生命周期方法改写为一个长尾?
- 内容介绍
- 文章标签
- 相关推荐
本文共计3146个文字,预计阅读时间需要13分钟。
plaintext创建React.js项目
gistfile1.txtcreate-react-js
# Installation: You’ll need to have Node >= 6 on your machine. You can use nvm to easily switch Node versions between different projects.
npm install -g create-react-app
# Creating an App
create-react-app my-app
cd my-app
It will create a directory called my-app inside the current folder.
Inside that directory, it will generate the initial project structure and install the transitive dependencies:
my-app
├── README.md
├── node_modules
├── package.json
├── .gitignore
├── public
│ └── favicon.ico
│ └── index.html
│ └── manifest.json
└── src
└── App.css
└── App.js
└── App.test.js
└── index.css
└── index.js
└── logo.svg
└── registerServiceWorker.js
npm start or yarn start
npm test or yarn test # Runs the test watcher in an interactive mode. By default, runs tests related to files changed since the last commit
npm run build or yarn build # Builds the app for production to the build folder.It correctly bundles React in production mode and optimizes the build for the best performance.
state 的主要作用是用于组件保存、控制、修改自己的可变状态。state 在组件内部初始化,可以被组件自身修改,而外部不能访问也不能修改。你可以认为 state 是一个局部的、只能被组件自身控制的数据源。
state 中状态可以通过 this.setState 方法进行更新,setState 会导致组件的重新渲染。
props 的主要作用是让使用该组件的父组件可以传入参数来配置该组件。它是外部传进来的配置参数,组件内部无法控制也无法修改。除非外部组件主动传入新的 props,否则组件的 props 永远保持不变。
但是它们的职责其实非常明晰分明:state 是让组件控制自己的状态,props 是让外部对组件自己进行配置。
没有 state 的组件叫无状态组件(stateless component),设置了 state 的叫做有状态组件(stateful component)。因为状态会带来管理的复杂性,我们尽量多地写无状态组件,尽量少地写有状态的组件。
import React,{ Component } from 'react'
import CommentInput from './CommentInput'
import CommentList from './CommentList'
class CommentApp extends Component{ //似乎只能靠console打印了
handleSubmitComment(content){
console.log(content)
}
render(){
return(
{this.props.comment.content}
) } } export default Comment /** * Created by Administrator on 2017/11/8. */ import React,{Component} from 'react' import Comment from './comment' class CommentList extends Component{ render(){ const comments = [ {username: 'Jerry', content: 'Hello'}, {username: 'Tomy', content: 'World'}, {username: 'Lucy', content: 'Good'} ] return( // { // comments.map((comment,i)=>{ // return( // {comment.username}:{comment.content} // ) // }) // } { comments.map((comment,i)=>React小书
) } } React通过this.state来访问state,通过this.setState()方法来更新state。当this.setState()方法被调用的时候,React会重新调用render方法来重新渲染UI setState方法通过一个队列机制实现state更新,当执行setState的时候,会将需要更新的state合并之后放入状态队列,而不会立即更新this.state(可以和浏览器的事件队列类比)。如果我们不使用setState而是使用this.state.key来修改,将不会触发组件的re-render。如果将this.state赋值给一个新的对象引用,那么其他不在对象上的state将不会被放入状态队列中,当下次调用setState并对状态队列进行合并时,直接造成了state丢失。 void setState( function|object nextState, [function callback] ) 第二个参数是一个回调函数,在setState的异步操作结束并且组件已经重新渲染的时候执行。也就是说,我们可以通过这个回调来拿到更新的state的值。 React也正是利用状态队列机制实现了setState的异步更新,避免频繁地重复更新state(pending的意思是未定的,即将发生的) this.setState 是在 render 时, state 才会改变调用的, 也就是说, setState 是异步的. 组件在还没有渲染之前, this.setState 还没有被调用.这么做的目的是为了提升性能, 在批量执行 State 转变时让 DOM 渲染更快. ReactDOM.render 是 React 的最基本方法,用于将模板转为 HTML 语言,并插入指定的 DOM 节点。 ReactDOM.render(Hello, world!
, document.getElementById('example') ); 上面代码将一个 h1 标题,插入 example 节点(查看 demo01),运行结果如下。 JSX 语法 var names = ['Alice', 'Emily', 'Kate']; ReactDOM.render( { names.map(function (name) { return Hello, {name}! }) } , document.getElementById('example') ); 上面代码体现了 JSX 的基本语法规则:遇到 HTML 标签(以 < 开头),就用 HTML 规则解析;遇到代码块(以 { 开头),就用 JavaScript 规则解析。上面代码的运行结果如下。 组件 React 允许将代码封装成组件(component),然后像插入普通 HTML 标签一样,在网页中插入这个组件。React.createClass 方法就用于生成一个组件类(查看 demo04)。 var HelloMessage = React.createClass({ render: function() { returnHello {this.props.name}
; } }); ReactDOM.render(-
{ React.Children.map(this.props.children, function (child) { return
- {child} ; }) }
{this.props.title}
; } }); 上面的Mytitle组件有一个title属性。PropTypes 告诉 React,这个 title 属性是必须的,而且它的值必须是字符串。现在,我们设置 title 属性的值是一个数值。 var data = 123; ReactDOM.render({this.props.title}
; } }); ReactDOM.render(You {text} this. Click to toggle.
); } }); ReactDOM.render(本文共计3146个文字,预计阅读时间需要13分钟。
plaintext创建React.js项目
gistfile1.txtcreate-react-js
# Installation: You’ll need to have Node >= 6 on your machine. You can use nvm to easily switch Node versions between different projects.
npm install -g create-react-app
# Creating an App
create-react-app my-app
cd my-app
It will create a directory called my-app inside the current folder.
Inside that directory, it will generate the initial project structure and install the transitive dependencies:
my-app
├── README.md
├── node_modules
├── package.json
├── .gitignore
├── public
│ └── favicon.ico
│ └── index.html
│ └── manifest.json
└── src
└── App.css
└── App.js
└── App.test.js
└── index.css
└── index.js
└── logo.svg
└── registerServiceWorker.js
npm start or yarn start
npm test or yarn test # Runs the test watcher in an interactive mode. By default, runs tests related to files changed since the last commit
npm run build or yarn build # Builds the app for production to the build folder.It correctly bundles React in production mode and optimizes the build for the best performance.
state 的主要作用是用于组件保存、控制、修改自己的可变状态。state 在组件内部初始化,可以被组件自身修改,而外部不能访问也不能修改。你可以认为 state 是一个局部的、只能被组件自身控制的数据源。
state 中状态可以通过 this.setState 方法进行更新,setState 会导致组件的重新渲染。
props 的主要作用是让使用该组件的父组件可以传入参数来配置该组件。它是外部传进来的配置参数,组件内部无法控制也无法修改。除非外部组件主动传入新的 props,否则组件的 props 永远保持不变。
但是它们的职责其实非常明晰分明:state 是让组件控制自己的状态,props 是让外部对组件自己进行配置。
没有 state 的组件叫无状态组件(stateless component),设置了 state 的叫做有状态组件(stateful component)。因为状态会带来管理的复杂性,我们尽量多地写无状态组件,尽量少地写有状态的组件。
import React,{ Component } from 'react'
import CommentInput from './CommentInput'
import CommentList from './CommentList'
class CommentApp extends Component{ //似乎只能靠console打印了
handleSubmitComment(content){
console.log(content)
}
render(){
return(
{this.props.comment.content}
) } } export default Comment /** * Created by Administrator on 2017/11/8. */ import React,{Component} from 'react' import Comment from './comment' class CommentList extends Component{ render(){ const comments = [ {username: 'Jerry', content: 'Hello'}, {username: 'Tomy', content: 'World'}, {username: 'Lucy', content: 'Good'} ] return( // { // comments.map((comment,i)=>{ // return( // {comment.username}:{comment.content} // ) // }) // } { comments.map((comment,i)=>React小书
) } } React通过this.state来访问state,通过this.setState()方法来更新state。当this.setState()方法被调用的时候,React会重新调用render方法来重新渲染UI setState方法通过一个队列机制实现state更新,当执行setState的时候,会将需要更新的state合并之后放入状态队列,而不会立即更新this.state(可以和浏览器的事件队列类比)。如果我们不使用setState而是使用this.state.key来修改,将不会触发组件的re-render。如果将this.state赋值给一个新的对象引用,那么其他不在对象上的state将不会被放入状态队列中,当下次调用setState并对状态队列进行合并时,直接造成了state丢失。 void setState( function|object nextState, [function callback] ) 第二个参数是一个回调函数,在setState的异步操作结束并且组件已经重新渲染的时候执行。也就是说,我们可以通过这个回调来拿到更新的state的值。 React也正是利用状态队列机制实现了setState的异步更新,避免频繁地重复更新state(pending的意思是未定的,即将发生的) this.setState 是在 render 时, state 才会改变调用的, 也就是说, setState 是异步的. 组件在还没有渲染之前, this.setState 还没有被调用.这么做的目的是为了提升性能, 在批量执行 State 转变时让 DOM 渲染更快. ReactDOM.render 是 React 的最基本方法,用于将模板转为 HTML 语言,并插入指定的 DOM 节点。 ReactDOM.render(Hello, world!
, document.getElementById('example') ); 上面代码将一个 h1 标题,插入 example 节点(查看 demo01),运行结果如下。 JSX 语法 var names = ['Alice', 'Emily', 'Kate']; ReactDOM.render( { names.map(function (name) { return Hello, {name}! }) } , document.getElementById('example') ); 上面代码体现了 JSX 的基本语法规则:遇到 HTML 标签(以 < 开头),就用 HTML 规则解析;遇到代码块(以 { 开头),就用 JavaScript 规则解析。上面代码的运行结果如下。 组件 React 允许将代码封装成组件(component),然后像插入普通 HTML 标签一样,在网页中插入这个组件。React.createClass 方法就用于生成一个组件类(查看 demo04)。 var HelloMessage = React.createClass({ render: function() { returnHello {this.props.name}
; } }); ReactDOM.render(-
{ React.Children.map(this.props.children, function (child) { return
- {child} ; }) }
{this.props.title}
; } }); 上面的Mytitle组件有一个title属性。PropTypes 告诉 React,这个 title 属性是必须的,而且它的值必须是字符串。现在,我们设置 title 属性的值是一个数值。 var data = 123; ReactDOM.render({this.props.title}
; } }); ReactDOM.render(You {text} this. Click to toggle.
); } }); ReactDOM.render(
