【发布时间】:2023-03-16 18:45:01
【问题描述】:
一些匹配数据来自父类,我使用 useState(match) 初始化匹配 但是,匹配数据包含旧数据,并未更新为父匹配数据。有人帮忙吗?
const FixtureDetailItem = ({ type, match, teams, isAdmin, postMatchesStart, putMatchesStart }) => {
console.log(match)
const [matches, setMatches] = useState(match);
const { homeTeamId, homeTeamName, homeScore, awayTeamId, awayTeamName, awayScore, scheduledAt, location, league, matchRecords} = matches;
useEffect(() => {
console.log('fired')
console.log(matches)
setMatches(matches)
}, [matches]);
【问题讨论】:
-
你做错了什么。当匹配项改变时,效果会重新触发,因为匹配项是它的依赖项。
-
也可以用
setMatches(matches)代替setMatches({ ...matches })。
标签: reactjs react-hooks