`
Dream丶小雅
  • 浏览: 47307 次
文章分类
社区版块
存档分类
最新评论

this.state与this.props的不同

阅读更多

React允许将代码封装成组件,然后像插入普通的HTML标签一样插入组件,在页面插入这个组件。React有个大的创新就是把组件看成一个状态机,一开始有个初始状态,然后用户互动,导致状态变化,从而触发渲染UI。getInitialState方法用于初始化,定义初始状态,也就是一个对象。这个对象可以通过this.state属性读取。当用户点击组件,导致状态变化,this.setState方法修改状态值,每次修改以后,自动调用this.render方法,再次渲染组件。

var LikeButton = React.createClass({
  getInitialState: function() {
    return {liked: false};
  },
  handleClick: function(event) {
    this.setState({liked: !this.state.liked});
  },
  render: function() {
    var text = this.state.liked ? 'like' : 'haven\'t liked';
    return (
      <p onClick={this.handleClick}>
        You {text} this. Click to toggle.
      </p>
    );
  }
});

ReactDOM.render(
  <LikeButton />,
  document.getElementById('example')
);

 由于 this.props 和 this.state 都用于描述组件的特性,可能会产生混淆。一个简单的区分方法是,this.props 表示那些一旦定义,就不再改变的特性,而 this.state 是会随着用户互动而产生变化的特性。

var HelloMessage = React.createClass({
  render: function() {
    return <h1>Hello {this.props.name}</h1>;
  }
});

ReactDOM.render(
  <HelloMessage name="John" />,
  document.getElementById('example')
);

 

分享到:
评论

相关推荐

    React中组件的props.children

    props.children是用于将从父组件传递过来的标签放置到子组件中 1.创建父组件 首先引入react和reactdom import React from 'react' import ReactDOM from 'react-dom' 创建父组件 class ParentDom extends React....

    ReactNative Navigation路由传递数据到下一个页面

    接着上面 Navigation 继续学习传递数据给下一个页面 ...this.props.navigation.state.params.movie 就可以拿到 电影对象 movie 接下来拿到电影信息,再去请求数据,展示在电影详情页,这里展示一下电影简介

    react ant Design手动设置表单的值操作

    this.props.form.setFieldsValue({ name:”张三”, }); 2.清空表单的值 this.props.form.resetFields(); 3.获取某一输入框的值 this.props.form.getFieldValue(‘newPassword’); 4.获取整个表单的值 this....

    es6 state&props设置大坑1

    this.state={ss:'xx',}}}这种默认prop和prop用static defaultPorps={x:'123',y:'312',}stati

    【JavaScript源代码】react实现Radio组件的示例代码.docx

    希望和大家一起学习,共同进步 效果展示: 测试组件: class Test extends Component { constructor(props) { super(props) this.state = { active:1 } } onGroupChange(value) { this.setState({ ...

    网络解析Httpery

    this.state = { arr: [] }; } componentDidMount() { //使用fetch用于网络加载数据 fetch( "http://api.shujuzhihui.cn/api/news/list?appKey=3e50f800ce7f4cdd9eda8d1c1ca71792&category;=娱乐" ) .then...

    ReactDemos:React演示以供学习

    **application:**利用this.props和this.state构建一个简单的应用 **external-plugins:**引用外部插件,构建一个简单的markdown编辑器 commentBox 一个较为完整的React入门实例 前端才用React构建commentBox组件,...

    详解三种方式在React中解决绑定this的作用域问题并传参

    在React中时常会遇到this指向的作用域问题 从而导致undefined报错 先来个Demo: ... import React from 'react';... this.state = { msg:"BindWithThis" } } render() { return &lt;input type="but

    leetcode答案-studyReact:研究React

    this.state = {isToggleOn: true}; // This binding is necessary to make `this` work in the callback this.handleClick = this.handleClick.bind(this); } handleClick() { this.setState(prevState =&gt; ({ ...

    React State(状态)

    React State(状态) React 把组件看成是一个状态机(State Machines)。...添加一个类构造函数来初始化状态 this.state,类组件应始终使用 props 调用基础构造函数。 React 实例 class Clock extends

    vdom-component

    虚拟组件基于virtual-dom构建的组件框架。 状态:实验安装npm i --save vdom-component用法var h = require('virtual-dom/h');var queue = require('vdom-render-queue');... this.props.render(); } }, v

    基于react框架使用的一些细节要点的思考

    4.组件类里有私有变量a,它到底改放在this.a中还是this.state对象中(作为属性a)呢? 1.setState到底是同步的还是异步的? class MyComponent extends React.Component{ constructor(props) { super(props) ...

    react-file-uploader

    ReactJS文件上传器 ... state = { files : [ ] , } ; this . uploadFiles = this . uploadFiles . bind ( this ) ; this . uploadFile = this . uploadFile . bind ( this ) ; } render ( ) {

    react-starter-es6

    react-starter-es6 裸机使用ES6启动使用JSX 为下 / 与与与现场重载的掉毛快速开始 $ npm install$ npm run dev命令npm run build为... state = props } handleClick ( ) { this . setState ( { n : this . state . n

    createtsy:一个Etsy副本,用户可以在其中搜索产品并将其添加到购物车并留下评论

    Component { constructor ( props ) { super ( props ) ; this . state = { search_query : "" } ; this . handleSubmit = this . handleSubmit . bind ( this ) ; this . update = this . update . bind ( this ) ...

    react-state-mutations:修改组件状态而无竞争条件

    class Counter extends Component { constructor ( props ) { super ( props ) ; this . state = { count : 0 } ; this . handleClick = this . handleClick . bind ( this ) ; } handleClick ( ) { this . setState...

    react-layer-cake

    什么啊 因此,您需要设置模式或某种“ outsideClickHandler”-我们将把您放在... this.state = { show: false } } handleClick(e) { this.setState({ show: !this.state.show }); } render() { var layer = n

    vue.js 子组件无法获取父组件store值的解决方式

    子组件: props:['myDetail'] 父组件: &lt;子组件 :myDetail="detail"&gt;&lt;/子组件&gt; computed:{ detail(){ ... this.detail=JSON.parse(JSON.stringify(this.$store.state.XXXX.yyyy)); } } 调用

    react 父子组件之间通讯props

    父组件中定义一个函数,包含一个props的参数,函数内利用super(props)传递给子组件,this.state中用于定义本页面中要用到的以及要传递给子组件的变量。 父组件的render函数中利用&lt;Table list={this.state.list}/...

    react-day-input:由Moment提供支持的简单日期选择器,用于做出React

    然后要求并与ES6导入配合使用: import React from 'react' import { StrictDayInput } from 'react-day-input' class MyForm extends React . Component { constructor ( props ) { super ( props ) ; this . ...

Global site tag (gtag.js) - Google Analytics