简体   繁体   English

如何避免刷新后显示 state 数据?

[英]How to avoid state data from being shown after refresh?

I have 2 components, A and B.我有 2 个组件,A 和 B。

  • In component B, I am getting a value from an API request that I am sending to component A on click of a button present in B.在组件 B 中,我从 API 请求中获取一个值,我通过单击 B 中存在的按钮将其发送到组件 A。

  • In component B:在组件 B 中:

     axios.get('<api url>').then(res => { this.props.navigate('/', { data: res.data.successMsg }) })

So on click of button in B, if the API works properly, I will be redirected to component A, and I can use this successMsg to show an alert with success message.因此,单击 B 中的按钮时,如果successMsg工作正常,我将被重定向到组件 A,我可以使用此成功消息显示带有成功消息的警报。 Till here it is fine.到这里为止还好。

Now When I refresh this page, it again shows the success message.现在,当我刷新此页面时,它再次显示成功消息。 I use the useLocation hook in component A to check for the state:我使用组件 A 中的 useLocation 挂钩来检查 state:

import { useLocation } form 'react-router-dom';

export function B(){
  const [showAlert, setShowAlert] = useState(false)
  const location1 = useLocation();

  useEffect(() => {
    if (location1.state !== null) {
      setShowAlert(true)
      window.setTimeout(() => {
        location1.state = null
      }, 8000)
    }
  }, [])
}

export default B

This if statement also work every time I refresh the page, so it makes no difference and renders the success message on every reload, which I don't want.这个if语句在我每次刷新页面时也有效,所以它没有任何区别,并且在每次重新加载时都会呈现成功消息,这是我不想要的。 I just want the success alert to show after redirection, ie button click.我只想在重定向后显示成功警报,即单击按钮。 How do I differentiate between refresh and redirect so that I can stop the message on refresh?如何区分刷新和重定向,以便在刷新时停止消息? or if there is some of other option/idea, please let me know.或者如果有其他选择/想法,请告诉我。 Note: I already tried the performance option, but on both redirect and refresh, it showed me the 'reload' option only.注意:我已经尝试过performance选项,但是在重定向和刷新时,它只显示了“重新加载”选项。

You can differentiate between the two using the window object:您可以使用window object 区分两者:

if (window.performance && (window.performance.navigation.type === "1")) {
    //It was a refresh
} else {
    //Something else
}

Alternatively you can use this:或者,您可以使用这个:

if (window.performance.getEntriesByType("navigation")[0].type === "reload") {
    //reload
} else {
    //something else
}

You can't mutate the location object.您不能改变location object。 You will need to manually clear the passed route state in the target component.您将需要手动清除目标组件中传递的路由 state。 Issue a redirect to the current path sans any state.在没有任何 state 的情况下发出到当前路径的重定向。

Example:例子:

import { useLocation, useNavigate } form 'react-router-dom';

export function B(){
  const { pathname, state } = useLocation();
  const navigate = useNavigate();

  const [showAlert, setShowAlert] = useState(state !== null);

  useEffect(() => {
    if (showAlert) {
      window.setTimeout(() => {
        navigate(pathname, { replace: true });
      }, 8000);
    }
  },[])

  ...
}

export default B;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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