欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

react踩坑记录

程序员文章站 2024-01-16 20:54:10
...

1

Multiple configuration files found. Please remove one:

如果你在根目录下自己建了个babel的配置文件 .babelrc,然后又好死不死的npm run eject把配置打开
package.json里面就又了babel的配置了,删掉任意一处配置即可

2 react useHistory hook 返回 undefined

useHistory won’t work in the component where you have your Routes because the context which is needed for useHistory is not yet set. useHistory will work on any child component or components which your have declared in your but it won’t work on 's parent component or component itself

主要还是上下文的关系,也就是用useHistory hook的组件要被包裹在Router组件内 ,这样上下文context就可以获取到history,仔细想想其实就是个js作用域的问题。

3 React this.props.history.push 链接变了页面没变
参考https://returnc.com/detail/3776
主要是router的结构要弄清楚,Router只有一个不要包裹多层

<Router>
	<Route/>
	....
</Router>
相关标签: 踩坑系列