javascript - TypeError:无法读取 react^16.13.1 react-router-dom^5.2.0 中未定义的属性“位置”
问题描述
我正在尝试使用“react”在反应路由器之间添加动画:“^16.13.1”、“react-router-dom”:“^5.2.0”、“react-transition-group”:“^4.4.1” . 但是出现错误说“TypeError:无法读取未定义的属性'位置'”,如何解决?
MainComponent.js
...
return (
<div>
<Header />
<TransitionGroup>
<CSSTransition key={this.pops.location.key} classNames="page" timeout={300}>
<Switch>
<Route path='/home' >{HomePage}</Route>
<Route exact path='/contactus' component={() => <Contact resetFeedbackForm={this.props.resetFeedbackForm}/>} />
<Route exact path='/menu' component={() => <Menu dishes={this.props.dishes} />} />
<Route path='/menu/:dishId' component={DishWithId} />
<Route exact path='/aboutus' component={() => <About leaders={this.props.leaders} />} />
<Redirect to="/home" />
</Switch>
</CSSTransition>
</TransitionGroup>
<Footer />
</div>
);
}
...
如何获取位置密钥?
解决方案
感谢大家。我找到了答案。我将代码移动到AnimatedSwitch
我应用动画并用于withRouter(({ location })
获取位置的对象,它工作正常。
class Main extends Component{
render(){
.......
.......
const AnimatedSwitch = withRouter(({ location }) => (
<TransitionGroup>
<CSSTransition
key={location.key}
classNames="page"
timeout={1000}
>
<Switch>
<Route path='/home' >{HomePage}</Route>
<Route exact path='/contactus' component={() => <Contact resetFeedbackForm={this.props.resetFeedbackForm} />} />
<Route exact path='/menu' component={() => <Menu dishes={this.props.dishes} />} />
<Route path='/menu/:dishId' component={DishWithId} />
<Route exact path='/aboutus' component={() => <About leaders={this.props.leaders} />} />
<Redirect to="/home" />
</Switch>
</CSSTransition>
</TransitionGroup>
));
return (
<div>
<Header />
<AnimatedSwitch />
<Footer />
</div>
);
}
}
您可以在此处找到有关在反应路由器路由之间添加动画的更多信息
.......
意味着这里有更多代码
推荐阅读
- css - 在 Bootstrap 中更改颜色箭头
- node.js - node-gyp 安装成功但失败并出现错误:gyp ERR!堆栈错误
- java - 如何制作会发生变化的圆形碰撞箱?
- sql - 如何在不写 where 条件的情况下在查询中使用时间函数?
- android - 从 xml 调用 View Model 方法不会在 android 中调用问题
- android - 使用 Work Manager 更新网络请求的逻辑
- c++ - (Rcpp)armadillos abs() 函数在 std::abs 工作时与 c++ double 一起使用时输出错误值
- python - 在 Python 中遍历另一个脚本的不同类
- c# - 如何在当前方法中使用数据库事务?
- google-apps-script - 谷歌工作表按钮在移动设备上不起作用