【发布时间】:2020-04-27 04:12:29
【问题描述】:
下面是我的反应代码。我有每个人的 key 属性,但我仍然收到错误消息。
**
- App.js
**
import React, { useState } from "react";
import "./App.css";
import Person from "./Components/Person/Person.js";
const App = props => {
const [currentState, updatePersonState] = useState({
Persons: [
{ id: "1", name: "abc", age: "37", hobbies: "Cricket" },
{ id: "2", name: "xyz", age: "33", hobbies: "Cook" },
{ id: "3", name: "pqr", age: "07", hobbies: "Reading" }
],
showContent: true,
buttonText:'Hide Content'
});
let persons = "";
if (currentState.showContent) {
persons = currentState.Persons.map((person, index) => (
<div>
<Person
name={person.name}
age={person.age}
hobbies={person.hobbies}
**key={person.id}**
></Person>
</div>
));
}
const toggleHandler = (event, index) => {
const showContentNow = currentState.showContent;
updatePersonState({
...currentState,
showContent: !showContentNow,
buttonText:currentState.showContent?'Show Content':'Hide Content'
});
};
return (
<div className="App">
<h1>Styling Components Dynamically</h1>
<div>
<button onClick={toggleHandler}>{currentState.buttonText}</button>
</div>
<div> {persons}</div>
</div>
);
};
export default App;
【问题讨论】:
标签: reactjs react-hooks