添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
酷酷的手术刀  ·  多个python环境pip ...·  1 年前    · 
完美的牛肉面  ·  HTML DOM scrollHeight ...·  1 年前    · 
开心的炒饭  ·  opencv - What is the ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

When useState update then map loop is not working in array inside of object in useState. React js

Ask Question

When useState update then map loop is not working in array inside of object in useState. React js

import { useState } from "react";
import React from "react";
function Check() {
const [Children, setChildren] = useState({data:[], otherdata:{}});
 function handleChange(){
 Children["data"] = [...Children["data"], Children["data"].length]
 setChildren(Children)
 alert(Children["data"])
  return (<React.Fragment>
    <div>Check</div>
    {Children["data"].map(data => <div>map</div>)}
    <button
     onClick={handleChange}
    >Add List</button>
  </React.Fragment>);
export default Check
                You need to update the actual object, not just the array inside of it, otherwise, you'll have a reference to the same object and react won't rerender: setChildren({...Children, data: [...Children.data, Children.data.length]})
– Nick Parsons
                Jul 11, 2021 at 6:51
  const [Children, setChildren] = useState({ data: [], otherdata: {} });
  function handleChange() {
    setChildren({
      ...Children,
      data: [...Children["data"], Children["data"].length],
  return (
    <React.Fragment>
      <div>Check</div>
      {Children.data.map((ele) => (
        <div>{ele}</div>
      <button onClick={handleChange}>Add List</button>
    </React.Fragment>
export default Check;

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question . Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers .