英文:
How to preserve the state of a child component in react?
问题
这是你的代码的翻译部分:
这里我创建了一个程序来接受不同的分数,有一个增加按钮,但问题是我想要增加 Rik 的分数 1 并点击下一个玩家 Sam 0,如果我再次点击下一个玩家,我希望看到 Rik 的分数 1,但目前显示的是分数 0,所以在父级渲染下如何保留子级的状态。我可以使用本地存储或上下文来提供建议吗?
import { useState } from 'react';
export default function Scoreboard() {
const [isPlayerA, setIsPlayerA] = useState(true);
return (
<div>
{/* {isPlayerA ? (
<Counter person="Sam" />
) : (
<Counter person="Rik" />
)} */}
{isPlayerA && (
<Counter person="Sam" />
)}
{ !isPlayerA &&(
<Counter person="Rik" />
)}
<button onClick={() => {
setIsPlayerA(!isPlayerA);
}}>
下一位玩家
</button>
</div>
);
}
function Counter({ person }) {
const [score, setScore] = useState(0);
const [hover, setHover] = useState(false);
let className = 'counter';
if (hover) {
className += ' hover';
}
return (
<div
className={className}
onPointerEnter={() => setHover(true)}
onPointerLeave={() => setHover(false)}
>
<h1>{person} 的分数: {score}</h1>
<button onClick={() => setScore(score + 1)}>
增加一分
</button>
</div>
);
}
请注意,我已经将代码中的注释翻译为中文。
英文:
Here I had created a programme to take a different score, and one increment button is present, but the problem is that I want to increment Rik's score 1 and click the next player, Sam 0, and if I again click on the next player, I want to see Rik's score 1, but currently it's showing the score 0, so how to preserve the state of the child when parent rendering is given below. Can I use local storage or context to give any suggestions?
import { useState } from 'react';
export default function Scoreboard() {
const [isPlayerA, setIsPlayerA] = useState(true);
return (
<div>
{/* {isPlayerA ? (
<Counter person="Sam" />
) : (
<Counter person="Rik" />
)} */}
{isPlayerA && (
<Counter person="Sam" />
) }
{ !isPlayerA &&(
<Counter person="Rik" />
)}
<button onClick={() => {
setIsPlayerA(!isPlayerA);
}}>
Next player
</button>
</div>
);
}
function Counter({ person }) {
const [score, setScore] = useState(0);
const [hover, setHover] = useState(false);
let className = 'counter';
if (hover) {
className += ' hover';
}
return (
<div
className={className}
onPointerEnter={() => setHover(true)}
onPointerLeave={() => setHover(false)}
>
<h1>{person}'s score: {score}</h1>
<button onClick={() => setScore(score + 1)}>
Add one
</button>
</div>
);
}
答案1
得分: 1
In any case, I think it is a good idea to store the score in the component <ScoreBoard/>
Because based on your current code, the component <Counter/>
will be re-rendered based on the value in isPlayerA
. So the score is reset every time you change players.
我认为无论如何,将得分存储在组件 <ScoreBoard/>
中都是一个好主意。因为根据你当前的代码,组件 <Counter/>
将根据 isPlayerA
中的值重新渲染。所以每次更换玩家时都会重置得分。
英文:
In any case, I think it is a good idea to store the score in the component <ScoreBoard/>
Because based on your current code, the component <Counter/>
will be re-rendered based on the value in isPlayerA
. So the score is reset every time you change players.
I have an alternative here, one drawback is that here I had to predefine the players in the score/setScore
hook.
import { useState } from 'react'
export default function Scoreboard() {
const [isPlayerA, setIsPlayerA] = useState(true)
const [score, setScore] = useState({
Sam: 0,
Rik: 0
})
return (
<div>
{/* {isPlayerA ? (
<Counter person="Sam" />
) : (
<Counter person="Rik" />
)} */}
{isPlayerA && <Counter person="Sam" score={score} setScore={setScore} />}
{!isPlayerA && <Counter person="Rik" score={score} setScore={setScore} />}
<button
onClick={() => {
setIsPlayerA(!isPlayerA)
}}
>
Next player
</button>
</div>
)
}
function Counter({ person, score, setScore }) {
const [hover, setHover] = useState(false)
let className = 'counter'
if (hover) {
className += ' hover'
}
return (
<div
className={className}
onPointerEnter={() => setHover(true)}
onPointerLeave={() => setHover(false)}
>
<h1>
{person}'s score: {score[person]}
</h1>
<button onClick={() => setScore({ ...score, [person]: (score[person] += 1) })}>
Add one
</button>
</div>
)
}
答案2
得分: 1
使用本地存储或上下文是保留子组件 React 状态的一些方法。要实施这些技巧,请尝试以下步骤:
- 本地存储:使用它可以为您的数据存储需求提供很好的帮助。有了这个功能,您可以将有价值的文件和信息保存在用户的浏览器上,使事情更加高效。当用户想随时访问它而不必来回访问服务器时,它非常方便。因为它是 HTML5 标准的一部分,受到不同浏览器的支持,如 Safari、Chrome 和 Firefox。此外,它易于使用,并为程序存储重要数据提供额外的空间。使用本地存储,数据的持久性得到了保证,您可以轻松检索重要信息,减少麻烦。
使用本地存储是保存和访问浏览器中的玩家分数的巧妙方法。查看下面的修改后的代码,其中包含了这个功能。
每个玩家的分数将在此方法下记录并从本地存储中检索。这确保了即使重新呈现父组件,当前状态也会得到保留。
- 上下文在正确理解信息方面非常有用。它可以帮助我们解释某些短语或思想背后的含义,否则可能会模糊不清。通过上下文,我们可以更好地理解信息的传达,避免歧义。在解释某些内容时,考虑周围的情况或事件是至关重要的,以免误解。上下文在我们的理解中提供了更丰富的体验,是有效沟通和理解信息的重要因素。
要在更高级别管理状态并将其分发给子组件,一种替代方法是使用 React 上下文。以下是一个示例:
使用 useContext 钩子和 ScoreContext 提供程序,Counter 组件可以消耗分数状态,而无论如何更改都会得到保留。
英文:
Using local storage or context are some ways to preserve a child component's React state. To implement these techniques, try the following:
1)Local Storage: employing it can do wonders for your data storing needs. With this feature, you can save valuable files and information on the user's browser, which makes things more efficient. It comes in handy when users want to access it anytime without having to go back and forth to the server. Because it is a part of the HTML5 standard, it is supported by different browsers like Safari, Chrome, and Firefox. On top of that, it is easy to use and gives you extra space to store important data for your program. With local storage, data persistence is taken care of, and you can retrieve vital information without too much hassle.
Using local storage is a nifty way to save and access player scores from the browser. Check out the revised code below, which incorporates this feature.
import { useState, useEffect } from 'react';
export default function Scoreboard() {
const [isPlayerA, setIsPlayerA] = useState(true);
return (
<div>
{isPlayerA ? (
<Counter person="Sam" />
) : (
<Counter person="Rik" />
)}
<button onClick={() => setIsPlayerA(!isPlayerA)}>
Next player
</button>
</div>
);
}
function Counter({ person }) {
const [score, setScore] = useState(0);
const [hover, setHover] = useState(false);
let className = 'counter';
if (hover) {
className += ' hover';
}
useEffect(() => {
// Retrieve the score from local storage when the component mounts
const savedScore = localStorage.getItem(`${person}_score`);
if (savedScore) {
setScore(parseInt(savedScore, 10));
}
}, [person]);
useEffect(() => {
// Save the score to local storage whenever it changes
localStorage.setItem(`${person}_score`, score.toString());
}, [person, score]);
return (
<div
className={className}
onPointerEnter={() => setHover(true)}
onPointerLeave={() => setHover(false)}
>
<h1>{person}'s score: {score}</h1>
<button onClick={() => setScore(score + 1)}>
Add one
</button>
</div>
);
}
Each player's score will be recorded and retrieved from local storage under this approach. This guarantees that the current state will be preserved even if the parent component is re-rendered.
2)Context comes in handy when it comes to understanding information correctly. It can help us decipher the meaning behind certain phrases or ideas that may otherwise be obscure. With context, we can better understand the message that is being conveyed without ambiguity. It's essential to consider the surrounding circumstances or events when interpreting something, so as not to misinterpret it. Context provides a richer experience in our comprehension and is a vital element in communicating and understanding information effectively.
To manage state at a higher level and distribute it to child components, an alternative approach is to utilize React Context. An illustration is provided as follows:
import { useState, useContext, createContext } from 'react';
// Create a context to hold the score state
const ScoreContext = createContext();
export default function Scoreboard() {
const [isPlayerA, setIsPlayerA] = useState(true);
return (
<ScoreContext.Provider>
<div>
{isPlayerA ? (
<Counter person="Sam" />
) : (
<Counter person="Rik" />
)}
<button onClick={() => setIsPlayerA(!isPlayerA)}>
Next player
</button>
</div>
</ScoreContext.Provider>
);
}
function Counter({ person }) {
const [score, setScore] = useContext(ScoreContext);
const [hover, setHover] = useState(false);
let className = 'counter';
if (hover) {
className += ' hover';
}
return (
<div
className={className}
onPointerEnter={() => setHover(true)}
onPointerLeave={() => setHover(false)}
>
<h1>{person}'s score: {score}</h1>
<button onClick={() => setScore(score + 1)}>
Add one
</button>
</div>
);
}
Using the useContext hook and ScoreContext provider, the Counter component consumes the score state that will be preserved regardless of changes.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论