react 的props和state

props

  当前组件的组件标签身上的所有属性和子节点构成的集合;

  可以用来组件传递数据,一般用于父子组件之间;

  this.props对象的属性与组件的属性一一对应,但对于组件标签的子节点,this.props.children属性的值有三种可能:
      1、如果当前组件没有子节点,为underfined;
      2、如果有一个子节点,类型为object;
      3、如果是多个子节点,就为array。
react提供一个工具方法,React.Chilren来处理this.props.children。可以用React.Chilren.map来遍历所有子节点

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script src="js/react.js"></script>
        <script src="js/react-dom.js"></script>
        <script src="js/browser.min.js"></script>
    </head>
    <body>
        <div id="app">

        </div>
    </body>
</html>
<script type="text/babel">
    let Hello = React.createClass({
        render(){
            return(
                <div>
                    {
                        React.Children.map(this.props.children,function(value,index){
                          return <p>{value}</p>
                      })
                    }
                </div>
            )
        }
    })
    ReactDOM.render(
        <Hello>
            <span>hello</span>
        </Hello>,
        document.getElementById("app")
    )
</script>

state和props一样。props是一个静态值,一旦设定了就不需要改变,一般用于设定请求的网络地址。

state是状态值,可以通过this.setState方法修改状态值,每次修改后,自动调用this.render方法,再次渲染组件。

getInitialState()方法用于定义初始状态。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script src="js/react.js"></script>
        <script src="js/react-dom.js"></script>
        <script src="js/browser.min.js"></script>
    </head>
    <body>
        <div id="app">

        </div>
    </body>
</html>
<script type="text/babel">
    let Hello = React.createClass({
        getInitialState(){     /*初始化状态值*/
            return{
                content:"hello"
            }
        },
        update(){
            this.setState({            /*修改状态值*/
                content:"Hello World"
            })
        },
        render(){
            return(
                <div>
                    <p>{this.state.content}</p>
                    <button onClick={this.update}>点击修改</button>
                </div>
            )
        }
    });
    ReactDOM.render(
        <Hello/>,
        document.getElementById("app")
    )
</script>

原文地址:https://www.cnblogs.com/guiyh/p/9393447.html

时间: 2024-08-30 01:38:41

react 的props和state的相关文章

React中Props 和 State用法

React中Props 和 State用法 1.本质 一句话概括,props 是组件对外的接口,state 是组件对内的接口.组件内可以引用其他组件,组件之间的引用形成了一个树状结构(组件树),如果下层组件需要使用上层组件的数据或方法,上层组件就可以通过下层组件的props属性进行传递,因此props是组件对外的接口.组件除了使用上层组件传递的数据外,自身也可能需要维护管理数据,这就是组件对内的接口state.根据对外接口props 和对内接口state,组件计算出对应界面的UI=>UI = C

react关于props和state举个例子

var Counter = React.createClass({ getInitialState: function() { return {count: this.props.initialCount}; }, handleClick: function() { this.setState({count: this.state.count + 1}); }, render: function() { return <div onClick={this.handleClick}>{this.

React丨props,state 和 render

小记: 当组件的state和props发生改变的时候,render函数会重新执行一次. 当父组件的render执行了,所有的子组件的render都会执行一次. 参考: https://github.com/wangjianuo/blog/issues/4 原文地址:https://www.cnblogs.com/wangjianuo/p/11039830.html

React Native props &amp; state

今天又敲了一丁点代码,看了一下props和state的用法 原本以为state只是一个状态,但是又阅读了一下原文,才知道state是一组状态,这些状态是开发者自己定义的,都统一在state这个大类底下,跟props一样都是 this.props.propertyName this.state.stateName 这种形式,props和state是控制组件的两种类型,props是开发者自定义的组件参数,state表达的是一种状态用于控制组件的内容 /** * Sample React Native

React Native 快速入门之认识Props和State

眼下React Native(以后简称RN)越来越火,我也要投入到学习当中.对于一个前端来说,还是有些难度.因为本人觉得这是一个App开发的领域,自然是不同.编写本文的时候,RN的版本为0.21.0.我们马上以代码进入今天的学习. 'use strict'; import React, { AppRegistry, Component, StyleSheet, Text, View } from 'react-native'; class Hello extends Component { re

关于props和state(React)

React的数据模型分为共有数据和私有数据,共有数据可以在组件间进行传递,私有数据为当前组件私有.共有数据在React中使用props对象来调用,它包含标签所有的属性名称和属性值,props对象有三个特性,单向流动性.显示传递性和只读性.单向流动性是指React的数据只能由父组件传递到子组件,而不能由子组件传递到父组件:显示传递性是指必须明确地在子组件中通过属性赋值,数据才可以传递到子组件:只读性是指props数据是只读的,数据修改后并未改变原始的数据模型,而是会新生成一份数据模型,并将新的数据

A Bite Of React(2) Component, Props and State

component component:用户自己定义的元素 const element = <Welcome name="Sara" />; class Welcome extends React.Component { render() { return <h1>Hello, {this.props.name}</h1>; } } 遇到自己定义的component Welcom,React会将它的属性(name)作为对象传递给组建Welcom,即{

从 0 到 1 实现 React 系列 —— 组件和 state|props

看源码一个痛处是会陷进理不顺主干的困局中,本系列文章在实现一个 (x)react 的同时理顺 React 框架的主干内容(JSX/虚拟DOM/组件/...) 项目地址 组件即函数 在上一篇 JSX 和 Virtual DOM 中,解释了 JSX 渲染到界面的过程并实现了相应代码,代码调用如下所示: import React from 'react' import ReactDOM from 'react-dom' const element = ( <div className="titl

react事件绑定的三种常见方式以及解决Cannot update during an existing state transition (such as within `render`). Render methods should be a pure function of props and state问题思路

在 React 组件中,每个方法的上下文都会指向该组件的实例,即自动绑定 this 为当前组件. 而且 React 还会对这种引用进行缓存,以达到 CPU 和内存的优化.在使用 ES6 classes 或者纯 函数时,这种自动绑定就不复存在了,我们需要手动实现 this 的绑定. 1.bind方法进行绑定,这个方法可以帮助我们绑定事件处理器内的 this ,并可以向事件处理器中传 递参数,如下图清晰明了: bind方法绑定 2.箭头函数进行绑定,箭头函数不仅是函数的“语法糖”,它还自动绑定了定义