java中render用法_如何在React中不在render函数中使用setState
您不应该使用 componentWillReceiveProps ,因为在最新版本中,'s UNSAFE and it won'适用于React的异步呈现 .
还有其他方法!
在初始装载和后续更新时,在调用render方法之前调用getDerivedStateFromProps . 它应返回一个对象来更新状态,或者返回null以不更新任何内容 .
所以在你的情况下
...component code
static getDerivedStateFromProps(props,state) {
if (this.props.cost == nextProps.cost) {
// null means no update to state
return null;
}
// return object to update the state
return { theData: this.props.cost };
}
... rest of code
您也可以使用memoization,但在您的情况下,由您决定 . The link has one example where you can achieve the same result with memoization and getDerivedStateFromProps
例如,在改变道具后更新列表(搜索)你可以从这里:
static getDerivedStateFromProps(props, state) {
// Re-run the filter whenever the list array or filter text change.
// Note we need to store prevPropsList and prevFilterText to detect changes.
if (
props.list !== state.prevPropsList ||
state.prevFilterText !== state.filterText
) {
return {
prevPropsList: props.list,
prevFilterText: state.filterText,
filteredList: props.list.filter(item => item.text.includes(state.filterText))
};
}
return null;
}
对此:
import memoize from "memoize-one";
class Example extends Component {
// State only needs to hold the current filter text value:
state = { filterText: "" };
// Re-run the filter whenever the list array or filter text changes:
filter = memoize(
(list, filterText) => list.filter(item => item.text.includes(filterText))
);
handleChange = event => {
this.setState({ filterText: event.target.value });
};
render() {
// Calculate the latest filtered list. If these arguments haven't changed
// since the last render, `memoize-one` will reuse the last return value.
const filteredList = this.filter(this.props.list, this.state.filterText);
return (
- {filteredList.map(item =>
- {item.text})}
);
}
}
java中render用法_如何在React中不在render函数中使用setState相关推荐
- python中如何追加_如何在Python中将元素添加到列表中-追加,扩展和插入
在Python中使用列表时,您通常会希望向列表中添加新元素. Python列表数据类型具有三种添加元素的方法:append()-将单个元素追加到列表. extend() -将iterable的元素添加 ...
- python中setattr用法_详解Python的hasattr() getattr() setattr() 函数使用方法
hasattr(object, name) 判断一个对象里面是否有name属性或者name方法,返回BOOL值,有name特性返回True, 否则返回False. 需要注意的是name要用括号括起来 ...
- react 组件中使用组件_禁止使用React功能组件的7个理由
react 组件中使用组件 Are React's Functional Components Worth The Cost? React的功能组件值得吗? Update 5/31/19: React ...
- react网格生成_如何在React中构建实时可编辑数据网格
react网格生成 by Peter Mbanugo 彼得·姆巴努戈(Peter Mbanugo) 如何在React中构建实时可编辑数据网格 (How to Build a Real-time Edi ...
- react js 添加样式_如何在React JS Application中添加图像?
react js 添加样式 Hello! In this article, we will learn how to add images in React JS? I remember when I ...
- react 交互_如何在React应用程序中跟踪用户交互
react 交互 by Faouzi Oudouh 通过Faouzi Oudouh 如何在React应用程序中跟踪用户交互 (How to track user interactions in you ...
- react前端显示图片_如何在react项目中引用图片?
如何在react项目中引用图片?本文码云笔记将为大家整理在react项目中插入图片以及背景图片的方法,希望对需要的小伙伴提供一些参考. 在react项目中插入图片以及背景图片的方法共有2种: 1.im ...
- react入门代码_如何在React中构建温度控制应用程序-包括提示和入门代码
react入门代码 我们正在建立的 (What we're building) In this beginner React project, we're going to learn how to ...
- java aes密钥生成_如何在Java(Android)中生成与.Net中相同的AES密钥?
我需要从.Net WebService提供的salt和密码生成 Java( Android)中的AES密钥.我需要使用与.net生成的密钥相同的密钥和相同的密码和盐(使用Rfc2898DeriveBy ...
最新文章
- CentOS7.4-btrfs管理及使用
- IT规划的企业应用实践(6)研究背景 之 企业信息化建设的诉求
- python主要就业方向-【数据说话】当下的Python就业前景如何
- 继承static的注意点
- 天转凉了,注意保暖,好吗(需求规格说明书放在github了)
- C语言读入文件全部内容的方法
- 【Get 以太坊技能】CentOS 7 Geth 搭建私链
- 子集生成 --二进制法
- String Table MFC
- 苹果电脑 html5 视频,苹果Mac系统看HTML5视频教程介绍
- Asp.net Boilerplate 源码无法打开——找不到.net core sdk
- java和python和php_Java、Python和PHP三者的区别
- 产品经理笔试面试(题目+答案)
- The Backrooms - Level 0.2 - 我爱杏仁水
- 四、青龙面板 Nvjdc(诺兰)安装教程
- 基于php的开题报告,基于php的微信web应用开发开题报告.doc
- Linux文本处理三剑客sed详解(正则匹配、命令示例)
- Rust中,*const T和*mut T的区别是什么?
- java计算时针和分针的夹角_【Java算法】一天24小时中,时针和分针一共重合多少次?...
- 新华三“智・行中国2022”|大厂行动,如何擘画“内循环”的数字未来?
热门文章
- java:为什么字符串比较时要用equals方法而不是==运算符
- 信息学奥赛一本通 1190:上台阶 | OpenJudge NOI 2.3 3525:上台阶
- 信息学奥赛一本通 1067:整数的个数 | OpenJudge NOI 1.5 11
- 信息学奥赛一本通(1022:整型与布尔型的转换)
- 63 SD配置-交货凭证配置-定义交货的项目类别
- 2 PP配置-一般设置-为工厂分配工厂日历
- 26 MM配置-采购-采购申请-定义编码范围
- QT配置OpenCV(二):成功
- hbase hmaster启动起来就自动关闭
- java gps 距离计算_Java教程之地图中计算两个GPS坐标点的距离