Tim*_*Tim 3 reactjs react-router material-ui react-hooks
我花了几天时间查看类似的帖子,但没有解决方案。出于某种原因,当我使用setPostState(myState.posts);它时不会重新渲染组件。
我正在使用反应 ^16.10.2
下面是我的代码:
import React, {useState, useCallback} from 'react';
import {withStyles, makeStyles} from '@material-ui/core/styles';
import {Paper, TableRow, TableHead, TableCell, TableBody, Table, Badge, Fab} from '@material-ui/core'
import {myState} from '../../PubSub/pub-sub'
import ThumbUpIcon from '@material-ui/icons/ThumbUp';
import ThumbDownIcon from '@material-ui/icons/ThumbDown';
const StyledTableCell = withStyles(...))(TableCell);
const StyledTableRow = withStyles(...))(TableRow);
const useStyles = makeStyles(theme => (...));
export default props => {
console.log("++++++++++++++++Render Body+++++++++++++++++++++");
const classes = useStyles();
let [postState, setPostState] = useState(myState.posts);// why does setPostState not update badge count???? or re-render component???
let upVote = (id) => {
let objIndex = myState.posts.findIndex((obj => obj.id == id));
return (
<Fab key={"upVote4309lk" +id} color="primary" aria-label="add" className={classes.fab}
onClick={() => {
myState.posts[objIndex].up_vote++;
setPostState(myState.posts);//why does this not update badge count???? or re-render component???
}}>
<Badge key={"Ubadge"+objIndex} className={classes.margin} badgeContent={postState[objIndex].up_vote} color="primary"><
ThumbUpIcon> </ThumbUpIcon>
</Badge>
</Fab>
)
};
let downVote = (id) => {
let objIndex = myState.posts.findIndex((obj => obj.id == id));
return (
<Fab key={"downVote0940v" + id} color="primary" aria-label="add" className={classes.fab}
onClick={() => {
myState.posts[objIndex].down_vote++;
setPostState(myState.posts);//why does this not update badge count???? or re-render component???
}}>
<Badge className={classes.margin} badgeContent={myState.posts[objIndex].down_vote} color="primary"><
ThumbDownIcon> </ThumbDownIcon>
</Badge>
</Fab>
)
};
function filter(name) {
return name.toLowerCase().includes(props.searchData.title.toLowerCase());
}
function createData(title, description, user, up_votes, down_votes, id) {
if (filter(title, description, user, up_votes, down_votes)) {
return (
<StyledTableRow key={id + "tableKey"}>
<StyledTableCell>{title}</StyledTableCell>
< StyledTableCell>{description}</StyledTableCell>
<StyledTableCell>{user}</StyledTableCell>
<StyledTableCell>{upVote(id)}</StyledTableCell>
<StyledTableCell>{downVote(id)}</StyledTableCell>
</StyledTableRow>
)
}
}
const rows = myState.posts.map(
obj => createData(obj.title, obj.description, obj.user, obj.up_votes, obj.down_votes, obj.id)
);
return (
<Paper className={classes.root}>
<Table className={classes.table} aria-label="customized table">
<TableHead>
<TableRow>
<StyledTableCell>Title</StyledTableCell>
<StyledTableCell>Description</StyledTableCell>
<StyledTableCell>User</StyledTableCell>
<StyledTableCell>Up Votes</StyledTableCell>
<StyledTableCell>Down Votes</StyledTableCell>
</TableRow>
</TableHead>
<TableBody>
{rows.map(row => (row))}
</TableBody>
</Table>
</Paper>
);
}
Run Code Online (Sandbox Code Playgroud)
任何帮助都会很棒,谢谢!
在 React 中,组件仅在state更改时重新渲染,即prevState!==currentState无论是基于类的组件还是函数式组件。在您的情况下,您正在调用 setPosts 但它不会更改状态,因为您myState.posts在设置状态时分配了相同的对象。React 不会对对象执行深度相等性检查,而是仅比较处于状态的对象的引用。在您的情况下,引用永远不会随着您的变化而改变,并且在调用 setPosts 后prevState保持等于newState。
为了避免这个问题,在 react 中使用对象/数组设置状态时,您需要确保分配一个新的引用。所以比较prevState并currState返回false。有关更多详细信息,请参阅相等检查示例
访问和设置状态的正确方法:
// Set the initial value using myState.posts and then use the variable
// postState to access the posts and not myState.posts
const [postState, setPostState] = useState(myState.posts)
const makeUpVote = (objIndex) => {
// Make local variable posts to change and set posts while
// using spread operator to make sure we get a new array created instead
// of pointing to the same array in memory
const posts = [...postState]
posts[objIndex].up_vote++
setPostState(posts)
}
let upVote = id => {
// use postState to access instead of myState.posts
let objIndex = postState.findIndex(obj => obj.id == id)
return (
<Fab
// I would recommend creating separate functions to handle this
// instead of writing them inline.
onClick={() => makeUpVote(objIndex)}
></Fab>
)
}
Run Code Online (Sandbox Code Playgroud)
相等检查示例:
// Set the initial value using myState.posts and then use the variable
// postState to access the posts and not myState.posts
const [postState, setPostState] = useState(myState.posts)
const makeUpVote = (objIndex) => {
// Make local variable posts to change and set posts while
// using spread operator to make sure we get a new array created instead
// of pointing to the same array in memory
const posts = [...postState]
posts[objIndex].up_vote++
setPostState(posts)
}
let upVote = id => {
// use postState to access instead of myState.posts
let objIndex = postState.findIndex(obj => obj.id == id)
return (
<Fab
// I would recommend creating separate functions to handle this
// instead of writing them inline.
onClick={() => makeUpVote(objIndex)}
></Fab>
)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1614 次 |
| 最近记录: |