React & Special Props Warning
React & Special Props Warning
key & ref

demo
index.js:1 Warning: Comment:
keyis not a prop. Trying to access it will result inundefinedbeing returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://fb.me/react-special-props)
in Comment (at comment.jsx:85)
in div (at comment.jsx:83)
in CommentList (at with.jsx:33)
in Unknown (at App.js:43)
in div (at App.js:37)
in App (at src/index.js:9)
in StrictMode (at src/index.js:8)

import React, { Component } from 'react';
class Comment extends Component {
constructor(props) {
super(props);
}
render() {
const {
comment,
id,
key,// Warning: Comment: `key` is not a prop.
} = this.props;
console.log(` this.props =`, this.props);
console.log(` key =`, key, key === undefined);
return (
<div className="comment-box">
<p>Comment = {comment}</p>
<p>id = {id}</p>
<p>key = {key === undefined ? "undefined" : key}</p>
</div>
)
}
}
class CommentList extends React.Component {
constructor(props) {
super(props);
this.state = {
// "DataSource" is some global data source
// comments: DataSource.getComments(),
comments: [
{
msg: "comment 1",
id: "c1",
},
{
msg: "comment 2",
id: "c2",
},
{
msg: "comment 3",
id: "c3",
},
],
};
this.handleChange = this.handleChange.bind(this);
}
componentDidMount() {
// Subscribe to changes
// DataSource.addChangeListener(this.handleChange);
setTimeout(() => {
this.handleChange();
}, 7*1000);
}
componentWillUnmount() {
// Clean up listener
// DataSource.removeChangeListener(this.handleChange);
}
handleChange() {
// Update component state whenever the data source changes
this.setState({
// comments: DataSource.getComments(),
comments: [
{
msg: "new comment 1",
id: "c1",
},
{
msg: "new comment 2",
id: "c2",
},
{
msg: "new comment 3",
id: "c3",
},
],
});
}
render() {
return (
<div>
{this.state.comments.map((comment) => (
<Comment comment={comment.msg} id={comment.id} key={comment.id} />
))}
</div>
);
}
}
export default CommentList;
StrictMode
React render twice bug
https://www.cnblogs.com/xgqfrms/p/13732464.html
StrictMode
https://reactjs.org/docs/strict-mode.html
StrictMode 是用于突出显示应用程序中潜在问题的工具。
与Fragment一样,StrictMode不会呈现任何可见的UI。
它为后代激活其他检查和警告。
注意: 严格模式检查仅在开发模式下运行;它们不会影响生产。
import React from 'react';
function ExampleApplication() {
return (
<div>
<Header />
<React.StrictMode>
<div>
<ComponentOne />
<ComponentTwo />
</div>
</React.StrictMode>
<Footer />
</div>
);
}
demos
render twice bug
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);

render once OK
ReactDOM.render(
<>
<App />
</>,
document.getElementById('root')
);

refs
https://reactjs.org/warnings/special-props.html
https://reactjs.org/docs/strict-mode.html
xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
React & Special Props Warning的更多相关文章
- React中props
今天让我们开启新的篇章好吧,来搞一搞React,以下所有操作都是我个人的一些理解,如果有错吴还请指出,想要看更全的可以去React官网可能一下子好吧 昨天按摩没到位,导致今天身体不太行,撸码千万别苦了 ...
- React中Props 和 State用法
React中Props 和 State用法 1.本质 一句话概括,props 是组件对外的接口,state 是组件对内的接口.组件内可以引用其他组件,组件之间的引用形成了一个树状结构(组件树),如果下 ...
- react hooks & props change & pagination current bug
react hooks & props change & pagination current bug multi tables & pigination bug & ...
- React Native props & state
今天又敲了一丁点代码,看了一下props和state的用法 原本以为state只是一个状态,但是又阅读了一下原文,才知道state是一组状态,这些状态是开发者自己定义的,都统一在state这个大类底下 ...
- React中props.children和React.Children的区别
在React中,当涉及组件嵌套,在父组件中使用props.children把所有子组件显示出来.如下: function ParentComponent(props){ return ( <di ...
- React 之props属性
React 里有一个非常常用的模式就是对组件做一层抽象.组件对外公开一个简单的属性(Props)来实现功能,但内部细节可能有非常复杂的实现. 可以使用 JSX 展开属性 来合并现有的 props 和其 ...
- React中props和state相同点和不同点
朋友们,我想死你们了,最近这几天忙着和病魔作斗争所以没怎么写博客,今天感觉好点了,赶紧来写一波,就是这木敬业. 今天我们来讨论讨论props和state相同点和不同点 首先我来概要说明一下这两者 pr ...
- React Render Props 模式
概述 Render Props模式是一种非常灵活复用性非常高的模式,它可以把特定行为或功能封装成一个组件,提供给其他组件使用让其他组件拥有这样的能力,接下来我们一步一步来看React组件中如何实现这样 ...
- react的props验证
Props 验证使用 propTypes,它可以保证我们的应用组件被正确使用,React.PropTypes 提供很多验证器 (validator) 来验证传入数据是否有效. 当向 props 传入无 ...
随机推荐
- ldf和mdf文件怎么还原到sqlserver数据库
1.把mdf文件和ldf文件拷贝到数据库的默认路径C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA里:2.在sq ...
- 入 Go 必读:大型Go工程的项目结构及实战思考 原创 毛剑 QCon 今天
入 Go 必读:大型Go工程的项目结构及实战思考 原创 毛剑 QCon 今天
- CF1416D 做题心得
CF1416D 做题心得 上次在某trick中提到了这个题,一开始觉得太毒瘤没有写,现在把它补上了. 感觉实现这个东西,比单纯收获一个trick,收获的东西多太多了. 主要思路 它的主要trick是& ...
- Mapper查询技巧
Sql字段动态比较判断 <sql id="getUserInfoList_body"> SELECT * from userinfo <dynamic prepe ...
- Hadoop优势,组成的相关架构,大数据生态体系下的模式
Hadoop优势,组成的相关架构,大数据生态体系下的模式 一.Hadoop的优势 二.Hadoop的组成 2.1 HDFS架构 2.2 Yarn架构 2.3 MapReduce架构 三.大数据生态体系 ...
- C++类基本--随笔二
1 #include <iostream> 2 #include <string.h> 3 using namespace std; 4 5 class Internet 6 ...
- 编程排序的一个excel-据说玩的好的,年薪50万了。只是你没在对的地方
是一个excel 下面是百度网盘分享地址; http://pan.baidu.com/s/1kTwqRfL
- FortiGate防火墙办公网常用配置
1.建立主备机 2.配置端口 3.配置SD-WAN 4.新建上网路由 5.新建上网策略 6.建立与其他点的IPSec VPN或点对点专线
- 力扣643.子数组最大平均数I-C语言实现
题目 给定 n 个整数,找出平均数最大且长度为 k 的连续子数组,并输出该最大平均数. 示例: 输入:[1,12,-5,-6,50,3], k = 4 输出:12.75 解释:最大平均数 (12-5- ...
- 牛客挑战赛33 C 艾伦的立体机动装置(几何)
思路: 我们需要枚举展开多少条边 然后把上底面的点放到和下底面一个平面 然后算两点之间的距离 注意判断直线与线段是否有交点 #include <bits/stdc++.h> using n ...