用使用效率和map()填充数组

发布于 2025-01-30 16:46:22 字数 695 浏览 4 评论 0原文

我试图在每个渲染时给一个初始值, 这就是我目前得到的

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

耳根太软 2025-02-06 16:46:22

尝试在使用效果中创建一个新的本地数组,然后在映射之后,将其分配给这样的状态变量:

useEffect(() => {
   const tmpMarkets = markets.map((item) => ({
      id: item,
      isActiveMarket: true,
      brandName
   });
   setActiveMarket(tmpMarkets);
}, []);

您的第一个摘要正在连续重置每个循环中的状态变量,因此这就是为什么您没有获得正确的数组。

Try creating a new local array in the useEffect and then after mapping it, assign it to the state variable like this:

useEffect(() => {
   const tmpMarkets = markets.map((item) => ({
      id: item,
      isActiveMarket: true,
      brandName
   });
   setActiveMarket(tmpMarkets);
}, []);

Your first snippet is continuously resetting the state variable in each loop, so that's why you are not getting the right array.

暮年慕年 2025-02-06 16:46:22

您可以执行此操作,

  useEffect(() => {
   
    setActiveMarket(activeMarket => [
      ...activeMarket,
      ...markets.map((item, i) => ({
        id: item,
        isActiveMarket: i === 0,
        brandName,
      }))
    ]);
  });
}, []);

第一个示例不正确,因为您正在连续设置状态并触发组件的重新启动

you can do this

  useEffect(() => {
   
    setActiveMarket(activeMarket => [
      ...activeMarket,
      ...markets.map((item, i) => ({
        id: item,
        isActiveMarket: i === 0,
        brandName,
      }))
    ]);
  });
}, []);

your first example is not right because you are continuously setting the state and that triggers the rerendering of the component

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文