【发布时间】:2021-02-23 08:48:37
【问题描述】:
我找到元素的索引号,更改该索引中的值以免破坏数组中的顺序,将其作为新数组分派但未呈现订阅的组件。
var userPhotoIndex = userPhotos.findIndex(p => p.photoId === photoId)
if (userPhotoIndex > -1) {
userPhotos[userPhotoIndex].likeCount -= 1;
dispatch(getUserPhotosSuccess([...userPhotos]))
}
其实redux扩展的状态会发生变化,而订阅的组件没有变化。
挂钩
function LikeButton({ photo, photoId, setlikeCount }) {
const [isLike, setIsLike] = useState(false)
const dispatch = useDispatch()
const history = useHistory()
const isLogged = useSelector(state => state.isLoggedReducer);
const userPhotos = useSelector(state => state.userReducer.userPhotos);
const onClick = () => {
var fd = new FormData();
fd.append("photoId", photoId)
if (!isLike) {
axios.post(LIKE_API_URL, fd, { headers: authHeaderObj() }).then(() => {
setIsLike(!isLike);
setlikeCount(!isLike);
var userPhotoIndex = userPhotos.findIndex(p => p.photoId === photoId)
if (history.location.pathname.includes("me/" + profileFlowState.Likes)) {
if (userPhotoIndex > -1) {
userPhotos[userPhotoIndex].likeCount += 1;
dispatch(getUserPhotosSuccess([...userPhotos]))
}
}
}).catch(err => redirectErrPage(err, dispatch));
}
else {
axios.delete(deleteLikePath(photoId), { headers: authHeaderObj() }).then(() => {
setIsLike(!isLike);
setlikeCount(!isLike);
var userPhotoIndex = userPhotos.findIndex(p => p.photoId === photoId)
if (history.location.pathname.includes("me/" + profileFlowState.Likes)) {
if (userPhotoIndex > -1) {
userPhotos[userPhotoIndex].likeCount -= 1;
dispatch(getUserPhotosSuccess([...userPhotos]))
}
}
}).catch(err => redirectErrPage(err, dispatch));
}
}
useEffect(() => {
if (isLogged) {
axios.get(getIsLikePath(photoId), { headers: authHeaderObj() }).then(res => setIsLike(res.data))
}
}, [isLogged, photoId])
if (!isLogged) {
return <Button onClick={() => history.push("/login")} variant="outline-primary" style={{ borderRadius: 0 }} className="btn-sm">
<i className="fa fa-thumbs-up" style={{ fontSize: 16 }}></i> Beğen</Button>
}
return (
<Button onClick={onClick} variant={isLike ? "primary" : "outline-primary"} style={{ borderRadius: 0 }} className="btn-sm">
<i className="fa fa-thumbs-up" style={{ fontSize: 16 }}></i> Beğen</Button>
)}
下面我正在渲染一个订阅的钩子。
已订阅
function UserPhotos({ userId }) {
const dispatch = useDispatch();
const [isLoading, setIsLoading] = useState(true)
const setFalseIsLoading = () => setIsLoading(false);
useEffect(() => { dispatch(getUserPhotosApi(userId, setFalseIsLoading)); }, [userId, dispatch])
const userPhotos = useSelector(state => state.userReducer.userPhotos)
return <div className="mt-3">{isLoading ? <Loading /> : <div>
<MapPhotoCard removeButton={true} refreshPhotos={(id) => {
dispatch(getUserPhotosSuccess([...userPhotos.filter(p => p.photoId !== id)]))
}} photos={userPhotos} /></div>}
</div>}
【问题讨论】:
-
可以分享完整的组件代码吗?
userPhotos是状态变量吗? -
是的,我正在获取当前状态
const userPhotos = useSelector(state => state.userReducer.userPhotos); -
我需要完整的组件代码来提供帮助。首先,我可以说你永远不应该改变你的状态变量而不像
userPhotos[userPhotoIndex].likeCount -= 1;那样调度一个动作,这是有问题的。如果需要,您可以更新您的问题以包含更多代码 -
但我将它作为一个新数组发送。你不认为它应该改变吗?
-
@AliYıldızöz 你改变了组件中的 redux 状态,然后用改变的值调度一个动作。您应该从减速器返回一个新状态,here 是一些信息,您可以如何做到这一点。
标签: javascript arrays reactjs redux react-redux