react创建组件值state

import React from "react"
import ReactDOM from "react-dom"
import './index.css'
import * as serviceWorker from './serviceWorker'

const user = {
    name: "liming",
    age: 23,
    gender: "男"
}

class Hello extends React.Component {
    //state是可以修改状态属性的
    constructor() {
        super()
        this.state = {
            "name": "xiaohong"
        }
    }

    render() {
        this.state.age = 26
        return <div>这里是{this.state.name},年龄{this.state.age}</div>
    }
}

ReactDOM.render(
    <div>
        <Hello {...user}></Hello>
    </div>
    ,
    document.getElementById("root"))
serviceWorker.unregister();

props和state的区别

  • props一般是从外界传过来的
  • state是组件私有的(通过ajax获取的数据)
  • props都是只读的,state是可读可写的。
posted @ 2020-06-25 19:17  公众号python学习开发  阅读(32)  评论(0编辑  收藏