English 中文(简体)
a. 将物体财产从一纸 in中划入,用于在反应中加附数据
原标题:setting object property from api call to cached data in react

最新信息:在使用南类毒素生成独一无二的陶器后,发现在浏览器复读时,脂肪将给每个团队带来新胎。 无论哪一个团队在重新卸载后会较早节省费用,该机将再次排出数据,为同一小组生成新的南类毒素。 任何解决办法? 唯一可以认为,现在可以把数据放在另一个档案中,并且将来可能建立火店。

我正在建造一个Nba stat履带,我正在研究一个功能,使用户能够挽救一个团队,在窗子重载上,或者当用户重新阅读时,即希望浏览器能够检查一个偏好的人。 监查国家阵列,以检查一个小组是否列入有利物清单,并在数据被搁置时更新物体财产。 在窗户重载上,喜爱的团队仍然包含着有利的团队,但是,如果偏爱者的财产没有被确定。 团队队伍包括正在装入更新的小组阵列的小组。

fetch (/** api data */)
.then(res => res.json())
    .then(data => {
      const updatedTeams = data.response.map((team: Team) => {
        return {
          ...team,
          isFavorite: favoriteTeams.includes(team) 
        };
      });
      setTeams(updatedTeams);
    })
    .catch(err => window.alert("Could not load data. Refresh and try again"));
  }, [season]);

不要检查整个团队物体是否在小队状态中检查了该病房,而那也是潜水器。

问题回答

参看你试图利用物体平等,在<条码>上找到小组。 这从来都不会奏效。

假设你重新使用<代码>当地Storage,则储存一个简单的JSON系列季节+ID特性。

然后使用该密码财产。

// use a ref to prevent recalculating every render
const favoriteTeams = useRef<Set<string>>(
  new Set(JSON.parse(localStorage.getItem( favoriteTeams ) ??  [] )),
);
const [teams, setTeams] = useState<Team[]>([]);

useEffect(() => {
  fetch(/* api data */)
    .then((res) => (res.ok ? res.json() : Promise.reject(res)))
    .then(({ response }) => {
      setTeams(
        response.map((team) => ({
          ...team,
          isFavorite: favoriteTeams.current.has(`${season}_${team.id}`),
        })),
      );
    })
    .catch((err) => {
      console.warn(err);
      alert( Could not load data. Refresh and try again );
    });
}, [season]);

你们可以使用这样的东西,以吸引团队偏爱,尽管你的使用可能有所不同。

const toggleFavorite = (team: Team) => {
  const isFavourite = team.isFavourite;
  const key = `${season}_${team.id}`;

  setTeams((prev) =>
    prev.toSpliced(prev.indexOf(team), 1, {
      ...team,
      isFavorite: !isFavorite,
    }),
  );

  if (isFavourite) {
    favoriteTeams.current.delete(key);
  } else {
    favoriteTeams.current.add(key);
  }
  localStorage.setItem(
     favoriteTeams ,
    JSON.stringify([...favoriteTeams.current]),
  );
};




相关问题
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.

热门标签