用使用效率和map()填充数组
我试图在每个渲染时给一个初始值, 这就是我目前得到的
const [activemarket,setActivemarket] = usestate([]); const markets = ['de','es','fr','uk'],我在州的过滤后得到了价值观。 const countrycode = de,我在州的某些过滤后得到了价值。
我正在尝试使用这些值在每个渲染中填充数组 这是我暂时得到的,
useEffect(() => {
markets.map((item) => {
setActiveMarket([
...activeMarket,
{
id: item,
isActiveMarket: true,
brandName,
},
]);
});
}, []);
我一直保持空数阵列,我不知道我在做什么,因为这样做时,它确实有效,但只能与一个对象:
useEffect(() => {
setActiveMarket([
...activeMarket,
{
id: countryCode,
isActiveMarket: true,
brandName,
},
]);
}, []);
i am trying to give an array an initial value at every render,
This is what i got for the moment
const [activeMarket, setActiveMarket] = useState([]);
const Markets = [ 'DE', 'ES', 'FR', 'UK'], i got the values after some filtering in the state.session.
const CountryCode = DE, i got the value after some filtering in the state.session.
and i am trying to fill an array at every render using these values
this is what i got for the moment
useEffect(() => {
markets.map((item) => {
setActiveMarket([
...activeMarket,
{
id: item,
isActiveMarket: true,
brandName,
},
]);
});
}, []);
i keep getting an empty array, i don't know what i am doing wrong exactly cause when i did this it worked but only with one object:
useEffect(() => {
setActiveMarket([
...activeMarket,
{
id: countryCode,
isActiveMarket: true,
brandName,
},
]);
}, []);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试在使用效果中创建一个新的本地数组,然后在映射之后,将其分配给这样的状态变量:
您的第一个摘要正在连续重置每个循环中的状态变量,因此这就是为什么您没有获得正确的数组。
Try creating a new local array in the useEffect and then after mapping it, assign it to the state variable like this:
Your first snippet is continuously resetting the state variable in each loop, so that's why you are not getting the right array.
您可以执行此操作,
第一个示例不正确,因为您正在连续设置状态并触发组件的重新启动
you can do this
your first example is not right because you are continuously setting the state and that triggers the rerendering of the component