美文网首页
React学习笔记(3) State Props

React学习笔记(3) State Props

作者: nieniemin | 来源:发表于2019-06-20 14:44 被阅读0次

State,Props作为React组件的两大重要组成部分,有必要学习记录下。加深自己的印象。

  1. State,Props是什么?
    在熟悉State,Props,再先熟悉下React组件。In React, everything is a component。React核心思想是组件化,其中 组件 通过属性(props)状态(state)传递数据。把传统DOM结构分成一个个单独的,相互独立的并且可重用的组件无疑会提高开发人员的效率。组件的优点非常多,如何向组件提供必要的数据?有两种方式,他们就是今天要学习的Props和State。
    Props是Properties的简写。它们是只读组件,工作方式类似于HTML属性。Prop是一种将数据从父组件传递给子组件的方法,并且是逐层发送数据,直到它到达目标子组件。
class ParentComponent extends React.Component {
    render () {
        return (
            <div>
                <SonComponent name="我是父组件,子组件可以通过props.name获取到这句话!"/>
            </div>
        )
    }
}

class SonComponent extends React.Component {
    render () {
        return (
            <div>
                <div>我是子组件,通过props.name接收父组件的内容为:{this.props.name}</div>
                <GrandsonComponent value="我是子组件,孙子组件可以通过props.value获取到这句话!"/>
            </div>
        )
    }
}

class GrandsonComponent extends React.Component {
    render () {
        return (
            <div>
                <div>我是孙子组件,通过props.value接收子组件的内容为:{this.props.value}</div>
                <h1>逐级传递,不可通过props.name获取到父组件内容</h1>
                <div>我是孙子组件,无法通过props.name接收父组件的内容:{this.props.name}</div>
            </div>
        )
    }
}

ReactDOM.render(
    <ParentComponent/>,
    document.getElementById("myprops")
);
打印结果可以看出来props是父组件传递给子组件,并且是逐级传递

由上面的代码我们可知props是一个从外部传进组件的参数,主要作为就是从父组件向子组件传递数据,它具有可读性和不变性,只能通过外部组件主动传入新的props来重新渲染子组件,否则子组件的props以及展现形式不会改变。
如果单靠props,只能从父组件传递,意味着不能更改它们。这无疑是致命的。state就是用来处理更新。State是一个组件的UI数据模型,是组件渲染时的数据依据。
一个组件的显示形态可以由数据状态和外部参数所决定,外部参数也就是props,而数据状态就是state。
通过下面的代码了解下state的简单用法。

class StateComponent extends  React.Component{
    constructor() {
        super();
        this.state = {
            name : "this is state",
        }
    }
    render() {
        return (
            <div>
                {this.state.name}
            <div>
        )
    }
}

在组件初始化的时候,通过this.state给组件设定一个初始的state,在第一次render的时候就会用这个数据来渲染组件。
state不同于props的一点是,state是可以被改变的。不过,不可以直接通过this.state=的方式修改,而需要通过this.setState()方法修改state。

class StateComponent extends  React.Component {

    constructor() {
       super();
       this.state = {
           count : 0,
       }
    };

    render() {
        return (
            <div>
                {this.state.count}
            </div>
        )
    }
}

接下来渲染出一个button,每点击一下,counter就+1

class StateComponent extends  React.Component {

    constructor() {
        super();
        this.state = {
            counter: 0,
        }
    }

    handleClick = () => {
        const counter = this.state.counter;
        this.setState(val => ({ counter: counter + 1 }));

    }
    render() {
        return (
            <div>
                counter is: {this.state.counter}
                <button onClick={this.handleClick} >点我</button>
            </div>
        )
    }
}

通过上述代码我们了解到两者的主要区别:

  • 首先状态(state) 和 属性(props) 类似,都是一个组件所需要的一些数据集合,但是state是私有的,可以认为state是组件的“私有属性(或者是局部属性)”。
  • State是可变的,是一组用于反映组件UI变化的状态集合;
  • Props对于使用它的组件来说,是只读的,要想修改Props,只能通过该组件的父组件修改。
  • 在组件状态上移的场景中,父组件正是通过子组件的Props, 传递给子组件其所需要的状态。

相关文章

  • React学习笔记(3) State Props

    State,Props作为React组件的两大重要组成部分,有必要学习记录下。加深自己的印象。 State,Pro...

  • React props

    React Props state 和 props 主要的区别在于 props 是不可变的,而 state 可以根...

  • React中的props和state

    props和state this.props 由 React 本身设定, 而 this.state 具有特殊的含义...

  • React基础

    react 教程 react 组件介绍 react state 介绍 react Props 介绍 React:组...

  • React内部状态state

    state   React组件的数据分为两种:props和state,props是组件的对外接口,state是组件...

  • React学习笔记(二)

    state, props,render() 为什么说React是由数据驱动的? 当组件的state或者props发...

  • react中的state和props

    前面提过react中的state和props是react组件中的两大部分,有很多人分不清state和props,这...

  • react native学习笔记6——Props和State

    Props(属性)和State(状态)是React Native中很重要的两个概念。使用Props和State,结...

  • React属性(props)和状态(state)

    React属性(props)和状态(state) 一、属性(props) 属性props是由外部传入、描述性质的,...

  • react组件间通信

    react中的props和state props只读,用于组件之间传递信息,这个信息包括:数据和函数 state用...

网友评论

      本文标题:React学习笔记(3) State Props

      本文链接:https://www.haomeiwen.com/subject/pkrzfctx.html