1. <small id='ZlBfd'></small><noframes id='ZlBfd'>

    2. <i id='ZlBfd'><tr id='ZlBfd'><dt id='ZlBfd'><q id='ZlBfd'><span id='ZlBfd'><b id='ZlBfd'><form id='ZlBfd'><ins id='ZlBfd'></ins><ul id='ZlBfd'></ul><sub id='ZlBfd'></sub></form><legend id='ZlBfd'></legend><bdo id='ZlBfd'><pre id='ZlBfd'><center id='ZlBfd'></center></pre></bdo></b><th id='ZlBfd'></th></span></q></dt></tr></i><div id='ZlBfd'><tfoot id='ZlBfd'></tfoot><dl id='ZlBfd'><fieldset id='ZlBfd'></fieldset></dl></div>

        <tfoot id='ZlBfd'></tfoot>
        <legend id='ZlBfd'><style id='ZlBfd'><dir id='ZlBfd'><q id='ZlBfd'></q></dir></style></legend>
        • <bdo id='ZlBfd'></bdo><ul id='ZlBfd'></ul>
      1. 如何在 React 状态下更新对象

        时间:2024-04-19
      2. <tfoot id='ZQHN0'></tfoot>
            <legend id='ZQHN0'><style id='ZQHN0'><dir id='ZQHN0'><q id='ZQHN0'></q></dir></style></legend>
              <bdo id='ZQHN0'></bdo><ul id='ZQHN0'></ul>

              <small id='ZQHN0'></small><noframes id='ZQHN0'>

                <tbody id='ZQHN0'></tbody>

                1. <i id='ZQHN0'><tr id='ZQHN0'><dt id='ZQHN0'><q id='ZQHN0'><span id='ZQHN0'><b id='ZQHN0'><form id='ZQHN0'><ins id='ZQHN0'></ins><ul id='ZQHN0'></ul><sub id='ZQHN0'></sub></form><legend id='ZQHN0'></legend><bdo id='ZQHN0'><pre id='ZQHN0'><center id='ZQHN0'></center></pre></bdo></b><th id='ZQHN0'></th></span></q></dt></tr></i><div id='ZQHN0'><tfoot id='ZQHN0'></tfoot><dl id='ZQHN0'><fieldset id='ZQHN0'></fieldset></dl></div>
                2. 本文介绍了如何在 React 状态下更新对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在 JS 对象(不是数组)中有一个 users 的索引列表.它是 React 状态的一部分.

                  I have an indexed list of users in the JS object (not array). It's part of the React state.

                  {
                      1: { id: 1, name: "John" }
                      2: { id: 2, name: "Jim" }
                      3: { id: 3, name: "James" }
                  }
                  

                  最佳做法是:

                  1. 添加一个新用户 { id: 4, name: "Jane" },id (4) 作为键
                  2. 移除 id 为 2 的用户
                  3. 将用户 #2 的名称更改为Peter"

                  没有任何不可变的助手.我正在使用 Coffeescript 和 Underscore(所以 _.extend 没问题...).

                  Without any immutable helpers. I'm using Coffeescript and Underscore (so _.extend is ok...).

                  谢谢.

                  推荐答案

                  这就是我会做的

                  • 添加:var newUsers = _.extend({}, users, { 4: { id: 4, ... } })
                  • 删除:var newUsers = _.extend({}, users) 然后delete newUsers['2']
                  • 更改:var newUsers = _.extend({}, users) 然后 newUsers['2'].name = 'Peter'
                  • add: var newUsers = _.extend({}, users, { 4: { id: 4, ... } })
                  • remove: var newUsers = _.extend({}, users) then delete newUsers['2']
                  • change: var newUsers = _.extend({}, users) then newUsers['2'].name = 'Peter'

                  这篇关于如何在 React 状态下更新对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:“桶装填"Javascript 或咖啡脚本中的算法 下一篇:禁用按钮仍然监听点击事件

                  相关文章

                  <i id='61muN'><tr id='61muN'><dt id='61muN'><q id='61muN'><span id='61muN'><b id='61muN'><form id='61muN'><ins id='61muN'></ins><ul id='61muN'></ul><sub id='61muN'></sub></form><legend id='61muN'></legend><bdo id='61muN'><pre id='61muN'><center id='61muN'></center></pre></bdo></b><th id='61muN'></th></span></q></dt></tr></i><div id='61muN'><tfoot id='61muN'></tfoot><dl id='61muN'><fieldset id='61muN'></fieldset></dl></div>

                  <legend id='61muN'><style id='61muN'><dir id='61muN'><q id='61muN'></q></dir></style></legend>

                      <bdo id='61muN'></bdo><ul id='61muN'></ul>
                    1. <tfoot id='61muN'></tfoot>

                      <small id='61muN'></small><noframes id='61muN'>