reactjs - 这是从哪里来的:警告:列表中的每个孩子都应该有一个唯一的“关键”道具
问题描述
我尝试了这个CodeSandbox并在本地的 VCode 中尝试了它,但在日志中看不到这个警告来自哪里:
Warning: Each child in a list should have a unique "key" prop.
Check the render method of `App`. See https://reactjs.org/link/warning-keys for more information.
at CurrentForm (http://localhost:3000/static/js/main.chunk.js:1284:11)
at App (http://localhost:3000/static/js/main.chunk.js:1102:70)
我更改了CurrentForm
组件“Form.js” map
,所以它key
有一个使用“npm i uuid”之类的唯一 ID,但事实并非如此。
解决方案
Form
向组件添加密钥: Codesandbox
import React, { useReducer } from "react";
import ReactDOM from "react-dom";
import { Router } from "@reach/router";
import { Form, Result, Landing } from "./steps";
import { NavigationButtons } from "./components";
import { initialState, dataReducer, DataContext } from "./dataContext";
import { formConfig } from "./consts/formConfig";
import "./styles.css";
function App() {
return (
<div>
<DataContext.Provider value={useReducer(dataReducer, initialState)}>
<Router>
<Landing path="/" />
{formConfig.map(({ prevStep, ...props }, index) => (
<Form
key={index}
{...props}
render={(prevStep) => <NavigationButtons prevStep={prevStep} />}
/>
))}
<Result path="result" />
</Router>
</DataContext.Provider>
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
推荐阅读
- javascript - 模拟文本输入更改,就好像用户键入了特定键以触发 React 的 onChange 处理程序?
- html - 结合使用 BeautifulSoup 的 iframe 内部和外部的 html 解析
- python - Python中未使用的变量
- c++ - fstream 读写函数错误
- javascript - 如何在重定向时隐藏 React sweetalert 弹出窗口
- python - 具有恒定时间访问的高效循环缓冲区
- reactjs - 我正在尝试为表单验证创建一个自定义挂钩
- java - 从命令行 Java 传递文件
- git - 相当于带有 git 的 TFS 源代码控制浏览器?
- php - SwiftMailer 无法发送 TemplatedEmail , symfony 4