React中的状态提升

React中的状态提升概括来说,就是将多个组件需要共享的状态提升到它们最近的父组件上.在父组件上改变这个状态然后通过props分发给子组件.

import React from 'react'
class Child_1 extends React.Component{
    constructor(props){
        super(props)
    }
    render(){
        return (
            

{this.props.value+2}

) } } class Child_2 extends React.Component{ constructor(props){ super(props) } render(){ return (

{this.props.value+1}

) } } class Three extends React.Component { constructor(props){ super(props) this.state = { txt:"牛逼" } this.handleChange = this.handleChange.bind(this) } handleChange(e){ this.setState({ txt:e.target.value }) } render(){ return (

{this.state.txt}

) } } export default Three
React中的状态提升_第1张图片
20180219235424.png

你可能感兴趣的:(React中的状态提升)