使用后,在儿童组件中消耗状态时,问题是什么?
我有以下我无法解释的情况。
我要做的是从子组件中的父组件中使用状态变量。 当我使用“使用效率”调用将一些新值加载到此状态时,仅正确的第二个组件才能正确呈现。在此组件中,我使用“ props”变量访问值。
第一个组件仅渲染我最初在父级调用中设置的内容,而忽略了使用效率调用中的新状态设置。在此组件中,我最初使用相同的道具属性设置其状态。
为什么会发生?
这是渲染的代码和片段。
app.js
import './App.css';
import First from './First';
import Second from './Second';
import { useEffect, useState } from 'react';
function App() {
const [someVar, setSomeVar] = useState({dos:['a']});
useEffect(()=>{
console.log('useEffect')
setSomeVar({dos:['a', 'b', 'c']})
}, [])
return (
<div className="App">
<header className="App-header">
<First letters={someVar}></First>
<hr/>
<Second letters={someVar}></Second>
</header>
</div>
);
}
export default App;
first.js
import { useState } from "react";
export default function First(props) {
const [letters, setLetter] = useState(props.letters.dos);
return (
<div>
{letters.map((letter) => {
return <span key={letter}>{letter}</span>;
})}
</div>
);
}
second.js
export default function Second(props) {
return (
<div>
{props.letters.dos.map((letter) => {
return <span key={letter}>{letter}</span>;
})}
</div>
);
}
结果
I have the following situation that I can't explain.
What I'm trying to do, is to use an state variable from a parent component in a child component.
When I load some new values into this state using a 'useEffect' call, only the Second component renders correctly. In this component I use 'props' variable to access the values.
The First component only renders what I initially set in the 'useState' call of the parent, ignoring the new state set in the useEffect call. In this component I set its state initially using the same props property.
Why is this occurring?
Here is the code and a snippet of the render.
App.js
import './App.css';
import First from './First';
import Second from './Second';
import { useEffect, useState } from 'react';
function App() {
const [someVar, setSomeVar] = useState({dos:['a']});
useEffect(()=>{
console.log('useEffect')
setSomeVar({dos:['a', 'b', 'c']})
}, [])
return (
<div className="App">
<header className="App-header">
<First letters={someVar}></First>
<hr/>
<Second letters={someVar}></Second>
</header>
</div>
);
}
export default App;
First.js
import { useState } from "react";
export default function First(props) {
const [letters, setLetter] = useState(props.letters.dos);
return (
<div>
{letters.map((letter) => {
return <span key={letter}>{letter}</span>;
})}
</div>
);
}
Second.js
export default function Second(props) {
return (
<div>
{props.letters.dos.map((letter) => {
return <span key={letter}>{letter}</span>;
})}
</div>
);
}
Result
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅当组件是首先安装以确定初始状态值时,将传递给
usestate
的值仅由React使用。在每个将来的渲染上,该组件使用的状态值将从该组件自己的先前状态或对该组件的状态设置器的先前调用中获取。目前,
您将应用程序的状态分为两个完全独立的部分;一个在父组件中带有
somevar
,另一个在第一个子组件中带有Letters
。对这两个州中的任何一个的进一步变化都不会影响另一个国家。为了避免这种问题,并保持数据井井有条,通常不要在一个以上的地方重复状态是一个好主意。如果您希望子组件能够访问和操纵链接到父母的状态,请父母向下传递状态,如果需要,状态设置者,,,不放置另一个
usestate < /代码>在孩子中。
The value passed to
useState
is only used by React when a component is first mounted to determine the initial state value. On every future render, the state value used by that component will be taken from that component's own prior state, or from a prior call to that component's state setter.At the moment that you do
you're splitting up your app's state into two completely separate parts; one with
someVar
in the parent component, and another withletters
in the First child component. Further changes to either one of those states will not affect the other.To avoid this sort of problem, and to keep your data organized, it's usually a good idea to not duplicate state in more than one place. If you want a child component to be able to access and manipulate state linked to the parent, have the parent pass down the state, and if needed, the state setter, without putting another
useState
in the child.