[英]Set data in React hooks?
This is my code.这是我的代码。
const initialNoticeState = {
id: props,
title : '',
description : '',
image : '',
updated_by : user.firstName+' '+user.lastName,
priority : ''
};
const [currentNotice, setCurrentNotice] = useState(initialNoticeState);
const notice = useSelector(state => state.notices.currentNotice)
const [noticeImage, setNoticeImage] = useState("");
const [imgData, setImgData] = useState(null);
const dispatch = useDispatch();
useEffect(() => {
dispatch(noticeActions.getById(props.id))
}, [props.id]);
I want to setCurrentNotice
from notice
value, just after dispatch
finishes.我想在dispatch
完成后从notice
值设置setCurrentNotice
。
Here is the data of notice
:这是notice
的数据:
I want to setCurrentNotice from notice value, just after dispatch finishes.我想在调度完成后从通知值设置CurrentNotice。
useEffect can be used to execute functions when variable is changed. useEffect 可用于在变量更改时执行函数。
useEffect(() => {
setCurrentNotice(notice)
}, [notice]); //notice is the dependency
就这样
setCurrentNotice({your current notice data})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.