简体   繁体   中英

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.

Here is the data of notice :

在此处输入图片说明

I want to setCurrentNotice from notice value, just after dispatch finishes.

useEffect can be used to execute functions when variable is changed.

  useEffect(() => {
   setCurrentNotice(notice)
  }, [notice]); //notice is the dependency

就这样

setCurrentNotice({your current notice data})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM