在学习React的组件的时候,我也好奇组件间需要共享状态或通信的时候,React是如何处理的。在文档的QUICK START的提到Lifting State Up(状态提升),并不是什么新鲜东西。只是把几个组件需要共享的状态拿出来放到最近的父组件而已。然后通过传参的方式注入子组件,成为其props。由于子组件获取的状态state_inShare都来自父组件,因此各自的state_inShare是同步的。

In React, sharing state is accomplished by moving it up to the closest common ancestor of the components that need it. This is called "lifting state up".

这是处理简单的共享状态的一种方式。

当我们把需要共享的状态放到父组件之后。我们就不能在子组件内通过setState方法改变它了,只能在父组件内用setState方法改变。那我们怎么在子组件中调用父组件的setState方法呢?

React的方案是:

在父组件中定义一个处理方法(比如handle(newValue){}),里面执行对state的更新。并将之作为prop传给子组件。

在子组件内部,当状态需要改变时,就调用父组件传递过来的this.props.handle()方法。

比如:

父组件中定义了handle:

   //...
handle(t) {
this.setState({temperature:t})
}
//..
<TemperatureInput onTemperatureChange={this.handle} temperature={temperature} />

在子组件<TemperatureInput>中:

   //组件中定义处理程序,执行父组件props下来的方法
changeTemp(e) {
this.props.onTemperatureChange(e.target.value);
}
//用户交互时改变了Temperature
<input type="text" value={temperature} onChange={this.changeTemp} />

其实就是曲线救国嘛。。。。

React:Lifting State Up的更多相关文章

  1. react 中state与props

    react 中state与props 1.state与props props是只读属性,只有在组件被实例化的时候可以赋值,之后的任何时候都无法改变该值.如果试图修改该值时,控制台会报错 only re ...

  2. [React] Update State in React with Ramda's Evolve

    In this lesson we'll take a stateful React component and look at how we can refactor our setState ca ...

  3. React给state赋值的两种写法

    如果你看过React的官方文档,就会对怎么给局部state赋值有一定的了解.如下代码: class Test extends React.Component { constructor(props) ...

  4. Recoil & React official state management

    Recoil & React official state management Redux Recoil.js https://recoiljs.org/ A state managemen ...

  5. React & update state with props & Object.assign

    React & update state with props & Object.assign Object.assign({}, oldObj, newObj) https://re ...

  6. 七天接手react项目 —— state&事件处理&ref

    state&事件处理&ref 在 react 起步 一文中,我们学习了 react 相关知识:jsx.组件.props.本篇将继续研究 state.事件处理和ref. state St ...

  7. [React Fundamentals] State Basics

    State is used for properties on a component that will change, versus static properties that are pass ...

  8. [React Native] State and Touch Events -- TextInput, TouchableHighLight

    In React, components manage their own state. In this lesson, we'll walk through building a component ...

  9. [React] React Fundamentals: State Basics

    State is used for properties on a component that will change, versus static properties that are pass ...

随机推荐

  1. python学习24之异常

    '''''''''1.低级错误:纯语法错误2.中级错误:代码存在隐性错误,逻辑缺陷3.高级错误:软件面对不确定性的异常错误''''''一.捕获异常1.基本异常捕获语句try: #异常捕捉语句的开始 代 ...

  2. Python 如何实现 单实例

    出处:https://stackoverflow.com/questions/380870/make-sure-only-a-single-instance-of-a-program-is-runni ...

  3. Codeforces Round #632 (Div. 2) 题解

    空山新雨后,天气晚来秋. 明月松间照,清泉石上流. 竹喧归浣女,莲动下渔舟. 随意春芳歇,王孙自可留.--王维 A. Little Artem 网址:https://codeforces.com/co ...

  4. POJ1077 八数码问题

    题目:八数码 网址:http://poj.org/problem?id=1077 在一个3×3的网格中,1~8这8个数字和一个"X"恰好不重不漏地分布在这3×3的网格中. 例如: ...

  5. 【Linux常见命令】diff命令

    diff - compare files line by line diff命令用于比较文件的差异. diff以逐行的方式,比较文本文件的异同处. 如果指定要比较目录,则diff会比较目录中相同文件名 ...

  6. HDU 1248 寒冰王座(完全背包问题另类解法)

    寒冰王座 Problem Description 不死族的巫妖王发工资拉,死亡骑士拿到一张N元的钞票(记住,只有一张钞票),为了防止自己在战斗中频繁的死掉,他决定给自己买一些道具,于是他来到了地精商店 ...

  7. redis-py中的坑

    今天发现,使用redis-py从redis中获取的数据竟然是加密的. conn = redis.Redis(host='redis_serverip', port=6379, password='re ...

  8. D. Almost All Divisors(数学分解因子)

    其实这题并不难啊,但是分解因子的细节一定要小心. \(比如样例48,2是因子说明24也是因子,也就是说假如x存在\) \(那么x一定是因子中的最小数乘上最大数\) \(那我们现在去验证x是否存在,先拿 ...

  9. HDU1176(正推DP)

    时间和位置都可以决定这一秒捡到的馅饼数 不妨设\(dp[i][j]\)为在\(i\)秒\(j\)位置的最大收益 那么\(dp[0][5]=0\),dp数组的其他部分置成-1代表不能转移 那么对于第\( ...

  10. 05_CSS入门和高级技巧(3)

    上节课复习 !important不能影响就近原则,远的标签如果加上!important也干不过近的标签! !important不能影响继承权重是0,通过继承的标签加上!important也干不过直接选 ...