React:Lifting State Up
在学习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的更多相关文章
- react 中state与props
react 中state与props 1.state与props props是只读属性,只有在组件被实例化的时候可以赋值,之后的任何时候都无法改变该值.如果试图修改该值时,控制台会报错 only re ...
- [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 ...
- React给state赋值的两种写法
如果你看过React的官方文档,就会对怎么给局部state赋值有一定的了解.如下代码: class Test extends React.Component { constructor(props) ...
- Recoil & React official state management
Recoil & React official state management Redux Recoil.js https://recoiljs.org/ A state managemen ...
- React & update state with props & Object.assign
React & update state with props & Object.assign Object.assign({}, oldObj, newObj) https://re ...
- 七天接手react项目 —— state&事件处理&ref
state&事件处理&ref 在 react 起步 一文中,我们学习了 react 相关知识:jsx.组件.props.本篇将继续研究 state.事件处理和ref. state St ...
- [React Fundamentals] State Basics
State is used for properties on a component that will change, versus static properties that are pass ...
- [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 ...
- [React] React Fundamentals: State Basics
State is used for properties on a component that will change, versus static properties that are pass ...
随机推荐
- 负载均衡服务之HAProxy基础配置(五)
前文我们聊了下haproxy的修改报文首部的配置.压缩功能以及haproxy基于http协议自定义健康状态检测机制:回顾请参考https://www.cnblogs.com/qiuhom-1874/p ...
- thinkphp5.0--auth权限
一般auth权限有四个表: think_admin:注册表, think_auth_group_access:用户组明细表(关联), think_auth_group:用户组表, think_arut ...
- weblogic补丁升级详细步骤,18.7.17补丁更新
weblogic打补丁 到weblogic官网下载补丁包 对应的补丁包 如: p22248372_1036012_Generic.zip 一 安装补丁步骤 1.登录linux的weblogic用户 ...
- opencv-9-图像噪声以及评估指标 PSNR 与SSIM
开始之前 我们在将 opencv 的图像显示在了 qt 的label 上, 我们能够将图显示在label 上, 用于显示我们的算法, 我们在 opencv 上一篇文章中介绍了 opencv 的核操作, ...
- Linux网络服务第三章远程访问及控制
1.笔记 655355:端口限制 监听地址:对外提供服务的地址 AllowUsers:仅允许用户登录 DenyUsers:仅禁止用户登录 AllowUsers-用户名-公网地址 ssh/id_rsa. ...
- 学习web前端的roadmap
- 热门云服务超87GB电子邮箱和密码泄露,黑客已验证大部分数据
热门云存储服务Mega被曝发现超87GB电子邮件地址和密码泄露(源数据目前已被删除,但已流传到个别黑客网站),其中包含近7.73亿电子邮件地址和2200万密码. 近日,国外一名安全研究人员Troy H ...
- Linux开发初探
坚持用了十几天的Linux操作系统,学会了很多的东西,但现在必须得抉择如何选择开发工具.在这些天的开发中,各种Linux下的IDE都有 所尝试.一向看好的Code::Blocks还是过于简单,用了一阵 ...
- Visual Studio Code插件安装步骤
1.进入扩展视图视图安装或卸载(快捷键Ctrl+shift+x) 转载于:https://www.cnblogs.com/SakalakaZ/p/7725159.html
- 数学--数论--HDU 5382 GCD?LCM?(详细推导,不懂打我)
Describtion First we define: (1) lcm(a,b), the least common multiple of two integers a and b, is the ...