Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么window.history.pushState更新我的内容而不是URL_Javascript_Reactjs_Function_History - Fatal编程技术网

Javascript 为什么window.history.pushState更新我的内容而不是URL

Javascript 为什么window.history.pushState更新我的内容而不是URL,javascript,reactjs,function,history,Javascript,Reactjs,Function,History,我希望能够在我的客户端应用程序上切换语言,而无需重新加载页面。我能够做到这一点,但我的url并没有像以前那样改变 我有两种语言-法语-/fr,英语-/en 这是我用window.history.pushState function generateUrlSwitch(lang) { const location = window.location.pathname; if (location.length > 3) { const actualPath =

我希望能够在我的客户端应用程序上切换语言,而无需重新加载页面。我能够做到这一点,但我的url并没有像以前那样改变

我有两种语言-法语-
/fr
英语-
/en

这是我用
window.history.pushState

function generateUrlSwitch(lang) {
    const location = window.location.pathname;
    if (location.length > 3) {
        const actualPath = location.substr(4, location.length);
        const url = `/${lang}/${actualPath}`;
         return window.history.pushState(null, null, url);
    } else {
        return '/' + lang;
    }
}
当前行为:

在不重新加载的情况下将内容更新到所选语言-url保持不变,但我可以看到参数在en/fr和I console.log之间以毫秒为单位切换,这两个选项都在console中

预期行为:

基于所选语言更新内容而不重新加载页面+基于所选语言更新url

语言切换器正在调用函数:

  href={generateUrlSwitch('en')}
  href={generateUrlSwitch('fr')}

我做错了什么?

不返回任何内容。也没有。我试图返回那里
lang
,但是输出是一样的,没有任何变化。