我是React的新手。
我从父数据中获取记录数组,并尝试使用setRecords更新记录数据。有没有办法从子状态更新父状态?
{
homeTeamName: "Blue"
homeTeamId: 1
homeScore: 1
awayTeamName: "Red"
awayTeamId: 2
awayScore: 1
scheduledAt: "2020-01-01T00:00:00"
location: "Fort Lee, NJ"
league: null
leagueId: 1
club: null
clubId: 1
matchRecords: Array(4)
0: {scoreMemberId: 2, scoreMemberName: "Jake Kwon2", scoreTeamId: 1, assistMemberId: 1, assistMemberName: "Jake Kwon1", …}
1: {scoreMemberId: 1, scoreMemberName: "Jake Kwon1", scoreTeamId: 1, assistMemberId: 2, assistMemberName: "Jake Kwon2", …}
2: {scoreMemberId: 3, scoreMemberName: "Jake Kwon3", scoreTeamId: 2, assistMemberId: 4, assistMemberName: "Jake Kwon4", …}
3: {scoreMemberId: 4, scoreMemberName: "Jake Kwon4", scoreTeamId: 2, assistMemberId: 3, assistMemberName: "Jake Kwon3", …}
}
const RecordItem = ({ record, teamId, memberOptions, isAdmin }) => {
const [records, setRecords] = useState(record);
const { scoreMemberId, scoreMemberName, assistMemberId, assistMemberName } = records;
useEffect(() => {
setRecords(record)
}, [record]);
<CustomDropdown
name='member'
value={scoreMemberId}
handleChange={member => setRecords({ ...records, scoreMemberId: member.value })}
options={memberOptions.filter(member => member.teamId === teamId)}
required
/>