English 中文(简体)
更新使用反馈功能的应变状态,并不将儿童部分改用最新的推进剂[封闭]
原标题:Updating a react state using callback function is not rerendering the child component with latest props [closed]
Closed. This question needs debugging details. It is not currently accepting answers.

我最近遇到了一个奇怪的问题,即反应状态装置和部件再补给。 我在母体部分有一个目标,我正在利用投入领域更新其价值,并将更新的国家变成儿童部分,以显示最新价值。 现在的问题是,在我用固定的国家方法更新国家时,它没有放过儿童部分,也没有显示最新的国家价值。

我知道,这只是通过在更新后建立一个新目标并通过它来制定国家方法,而我想知道这种使用的效果如何在背景中发挥作用,以及为什么在我使用回击时,它不会更新国家。

我在增加法典参考资料,以便更好地了解和理解。

百分比25 CODESANDBOX LINK with EXAMPLE

虽然我增加了说明这一问题的法典和工具箱的工作联系,但一些“Extra smart”和“extra intelligent”的人已经结束这一问题,并投了反对票,说它缺乏“伪造细节”。 因此,我在这里明确为这些伟大民族增添了法典。

import { useState } from "react"; import Heading from "./components/Heading"; import "./styles.css";

export default function App() {
  const [data, setData] = useState({});

  return (
    <div className="App">
      <h1>{data.label}</h1>
      <h2>Start editing to see some magic happen!</h2>
      <input
        type="text"
        placeholder="Enter value"
        onChange={(e) =>
          setData((prevState) => {
            prevState.label = e.target.value;
            return prevState;
          })
        }
      />
      <Heading data={data.label} />
    </div>
  );
}

export default function Heading({ data }) {
  return (
    <div className="App">
      <h1>{data}</h1>
      <h2>Start editing to see some magic happen!</h2>
    </div>
  );
}
问题回答
onChange={(e) =>
          setData((prevState) => {
            prevState.label = e.target.value;
            return prevState;
          })
        }

您将原物体保留在定国,“PrevSTate”。 仅凭act,就不是深 d。 这应当改为return{......prevState, 标签: e.target. Value };,以创建新的参考。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签