【问题标题】:Set string in URL (Reactjs)在 URL 中设置字符串(Reactjs)
【发布时间】:2021-10-11 07:30:47
【问题描述】:

例如,当我使用链接时:

https://stackoverflow.com/questions/11227809

它会自动转换为:

https://stackoverflow.com/questions/11227809/why-is-processing-a-sorted-array-faster...

如何在 Reactjs 中做同样的事情? (我的组件中已经有一个字符串)

【问题讨论】:

标签: javascript reactjs


【解决方案1】:

我的回答:

  const { id } = useParams(); // get id by react-route
  const history = useHistory();

  useEffect(() => {
    let exampleName = "string-by-id";  // get string name by id 
    history.push("/questions/"+id+"/"+exampleName);
  }, []);

【讨论】:

    【解决方案2】:

    一般:

    要重定向客户端(即基本上模拟他们点击链接),请使用window.location.href = 'my-domain/my-subdirectory'

    要替换客户端的 URL(即重定向它们而不将当前 URL 添加到浏览器历史记录),请使用 window.location.replace('my-domain/my-subdirectory')

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-16
      • 2023-03-27
      相关资源
      最近更新 更多