gpt4 book ai didi

reactjs - React 独特的 "key"错误

转载 作者:行者123 更新时间:2023-12-03 13:33:55 26 4
gpt4 key购买 nike

我正在浏览 React 中的待办事项列表教程,并遇到了以下错误,我花了很长时间,只是找不到错误。这是错误和组件的代码,这是是类(class)存储库的代码(问题出现在此提交上):

https://github.com/andrewjmead/react-course-todo-app/commit/0521f151705f78cb9f8d69262eb093f1431cb9ca

非常感谢任何帮助。

警告:数组或迭代器中的每个子项都应该有一个唯一的“key”属性。检查TodoList的渲染方法。请参阅 fb.me/react-warning-keys 了解更多信息。

终端中还有一个错误,对于 TOGGLE_TODO 情况下的扩展运算符

return {
...todo, // here
completed: nextCompleted,
completedAt: nextCompleted ? moment().unix() : undefined
};

var React = require('react');
var { connect } = require('react-redux');
import Todo from 'Todo';
var TodoAPI = require('TodoAPI');

export var TodoList = React.createClass ({
render: function() {
var { todos, showCompleted, searchText } = this.props;
var renderTodos = () => {
if(todos.length === 0) {
return (
<p className="container__message">No tasks</p>
);
}
return TodoAPI.filterTodos(todos, showCompleted, searchText).map((todo) => {
return (
//add unique key prop to keep track of individual components
<Todo key={todo.id} {...todo} />
);
});
};
return (
<div>
{renderTodos()}
</div>
);
}
});

export default connect(
(state) => {
return state;
}
)(TodoList);

reducer :

var uuid = require('uuid');
var moment = require('moment');

export var searchTextReducer = (state = '', action) => {
switch (action.type) {
case 'SET_SEARCH_TEXT':
return action.searchText;
default:
return state;
};
};

export var showCompletedReducer = (state = false, action) => {
switch (action.type) {
case 'TOGGLE_SHOW_COMPLETED':
return !state;
default:
return state;
};
};

export var todosReducer = (state = [], action) => {
switch(action.type) {
case 'ADD_TODO':
return [
...state,
{
text: action.text,
id: uuid(),
completed: false,
createdAt: moment().unix(),
completedAt: undefined
}
];
case 'TOGGLE_TODO':
return state.map((todo) => {
if(todo.id === action.id) {
var nextCompleted = !todo.completed;

return {
...todo,
completed: nextCompleted,
completedAt: nextCompleted ? moment().unix() : undefined
};
} else {
return todo;
}
});
case 'ADD_TODOS':
return [
...state,
...action.todos
];
default:
return state;
}
};


Webpack:

var webpack = require('webpack');

module.exports = {
entry: [
'script!jquery/dist/jquery.min.js',
'script!foundation-sites/dist/js/foundation.min.js',
'./app/app.jsx'
],
externals: {
jquery: 'jQuery'
},
plugins: [
new webpack.ProvidePlugin({
'$': 'jquery',
'jQuery': 'jquery'
})
],
output: {
path: __dirname,
filename: './public/bundle.js'
},
resolve: {
root: __dirname,
modulesDirectories: [
'node_modules',
'./app/components',
'./app/api'
],
alias: {
applicationStyles: 'app/styles/app.scss',
actions: 'app/actions/actions.jsx',
reducers: 'app/reducers/reducers.jsx',
configureStore: 'app/store/configureStore.jsx'
},
extensions: ['', '.js', '.jsx']
},
module: {
loaders: [
{
loader: 'babel-loader',
query: {
presets: ['react', 'es2015']
},
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/
}
]
},
devtool: 'cheap-module-eval-source-map'
};

最佳答案

在 React 中,当您渲染多个相同的组件(在您的例子中是待办事项)时,您需要为每个组件添加一个唯一的键,这是因为 React 需要知道如何在虚拟中处理它们统治。

您可以采取多种措施来解决此问题:

  1. 在 for 循环中,创建一个索引变量,并在每次循环结束时将其加 1,然后将其设置为每个渲染组件的键。

  2. 如果您从 API 获取待办事项,请为每个待办事项设置一个 ID 并将其用作组件 key 。

  3. 使用随机数生成器为每个待办事项设置唯一的 key 。

最好的方法是#2和#3,我发现在你的情况下,你正在尝试执行#2(通过todo id设置 key ),但我认为它是未定义的,请检查它。

另一个解决方案是使用uuid在每个渲染的组件/todo 上。

为此,您可以安装 node-uuid .

运行:npm i --save node-uuid

然后在文件中进行导入:import uuid from 'node-uuid'const uuid = require('node-uuid')

现在将您的代码更改为如下所示:

return TodoAPI.filterTodos(todos, showCompleted, searchText).map((todo) => {
return (
//add unique key prop to keep track of individual components
<Todo key={uuid()} {...todo} />
);
});

然后就可以开始了。

关于reactjs - React 独特的 "key"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43621629/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com