javascript - React Router V4 - 道具没有从父组件传递给 this.props.children
问题描述
使用 react-router v4 时,React.cloneElement 不会将道具传递给子组件。我正在获取历史和位置道具。只是没有从 mapStateToProps 获得任何动作或道具。
使用 react-router v3 时工作正常......仅当我更新版本时,道具没有被传递。
class Main extends React.Component{
render(){
var clonedElements = React.cloneElement(this.props.children, this.props);
return(
<div>
<h1>
<Link className='headLine' to='/'><img src='https://i.imgur.com/DFkO2gb.png'/></Link>
<p className='disclosure'>....</p>
</h1>
{clonedElements}
</div>
)
}
}
const router = (
<Provider store={store}>
<Router history={history}>
<App>
<Switch>
<Route exact render={props => <CardsGrid {...props} /> }></Route>
<Route exact path='/view/:postId' component={Single}></Route>
</Switch>
</App>
</Router>
</Provider>
)
function mapStateToProps(state){
return {
posts: state.posts,
comments: state.comments
}
}
function mapDispatchToProps(dispatch){
return bindActionCreators(actionCreators, dispatch);
}
const App = withRouter(connect(mapStateToProps, mapDispatchToProps)(Main));
export default App;
我期待看到初始状态设置的 2 个道具作为道具。我现在没有看到这些。
解决方案
推荐阅读
- matlab - 根据条件删除行(Matlab)
- python - Python 上的缩进错误
- java - 如何将数据库记录插入对象?
- python - 如何按组对所有变量使用描述()?
- laravel - 刀片文件中的 laravel 调用命令打印为零
- ffmpeg - 使用ffmpeg c代码的rtsp服务器
- angular - 启动 Angular Cli 服务器时出错
- android - 添加/修改孩子的通知
- go - Bcrypt hashedSecret 太短而不能成为 bcrypted 密码
- spring-boot - Spring Boot swagger UI 在 nginx 代理后面不起作用