[英]I am getting an Error: React.Children.only expected to receive a single React element child in my index.js page. Is there a workaround
Index.js code here Index.js 代码在这里
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
I am guessing the error is not comming from the code above and neither is it comming the App.js code.我猜错误不是来自上面的代码,也不是来自 App.js 代码。 App.js code here
App.js 代码在这里
function App() {
return (
<div className="App">
<header className="App-header">
<AppNavbar/>
<ShoppingList/>
</header>
</div>
);
}
Here's an excerpt from ShoppingList.js.这是 ShoppingList.js 的摘录。 Hope this is of help: I think this is where the problem originates because I changed
items.map
this.state.items.map
and the error was, items.map is not defined
. Hope this is of help: I think this is where the problem originates because I changed
items.map
this.state.items.map
and the error was, items.map is not defined
.
class ShoppingList extends Component {
state = {
items: [
{ id: uuid(), name: 'Eggs'},
{ id: uuid(), name: 'Milk'},
{ id: uuid(), name: 'Apples'},
{ id: uuid(), name: 'Water'},
]
}
render() {
const items= this.state;
return(
<container>
<Button
color="dark"
style={{marginBottom : '2rem'}}
onClick={() => {
const name = prompt("Enter item");
if (name) {
this.setState( state => ({
items: [...state.items, {id: uuid(), name}]
}));
}
}}
>Add Item</Button>
<ListGroup>
<TransitionGroup className="shopping-list">
{this.state.items.map(({ id, name}) => (
<CSSTransition key="{id}" timeout="500" classNames="fade">
</CSSTransition>
))}
</TransitionGroup>
</ListGroup>
</container>
)
}
}
I have checked many other errors similar to this but without any success.我检查了许多与此类似的其他错误,但没有任何成功。 Does anyone have a solution for this?
有人对此有解决方案吗?
<CSSTransition>
.<CSSTransition>
中。 So in your case, put your ListGroup.Item
and Button
inside CSSTransition
.ListGroup.Item
和Button
放在CSSTransition
中。key
(string) in <CSSTransition key="{id}"
.<CSSTransition key="{id}"
中使用了错误的key
(字符串)。 So use provide unique key - like this - <CSSTransition key={id}
<CSSTransition key={id}
Working copy of your code is here:您的代码的工作副本在这里:
https://codesandbox.io/s/wild-snow-gwilb?file=/src/ShoppingList.js https://codesandbox.io/s/wild-snow-gwilb?file=/src/ShoppingList.js
Try putting试着放
<AppNavbar/>
<ShoppingList/>
under <div></div>
tag as header might need single element under it.在
<div></div>
标签下作为 header 可能需要在它下面的单个元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.