使用 React 路由器更改基于 url 的组件
Changing components based on url with react router
这更多是关于 React 的体系结构问题,而不是特定问题,但被认为是管理 state/props 布局组件和基于 [=39] 呈现的多个子组件的最佳实践=]?
注意:我知道有人问过类似的问题,但这有点不同。 [How to update ReactJS component based on URL / path with React-Router
假设我有类似以下代码的内容:一个配置文件页面(主布局视图),其中包含配置文件子部分(设置、首选项、帐户详细信息等)的导航链接,以及一个主面板,其中每个子部分被呈现。
所以目前我会有这样的东西:
我的路由器
routes.js
<Router history={browserHistory}>
<Route path='/profile' component={Profile} >
<IndexRoute component={Summary} />
<Route path='/profile/settings' component={Settings} />
<Route path='/profile/account' component={Account} />
<Route path='/profile/preferences' component={Preferences} />
</Route>
</Router>
和我的个人资料布局组件的精简版
profile.js
class Profile extends React.Component {
constructor(props) {
super(props)
}
render(){
let pathName = this.props.location.pathname;
return(
<div className='container profile-page'>
<div className='side-nav'>
<ul>
<li><Link to='/profile'>Summary</Link></li>
<li><Link to='/profile/settings'>Settings</Link></li>
<li><Link to='/profile/account'>My Account</Link></li>
<li><Link to='/profile/preferences'>Preferences</Link></li>
</ul>
</div>
<div className='main-content'>
{this.props.children}
</div>
</div>
)
}
}
export default Profile;
所以这种作品。子组件将基于 url 进行渲染。但是我该如何管理状态和道具呢?按照我对 React 和 Flux 的理解,我希望 Profile 组件管理状态并监听我的商店的变化,并将这些变化传播给它的子组件。这个对吗?
我的问题是,似乎没有一种直观的方式将 props 传递给 this.props.children 渲染的组件,这让我觉得我当前的架构 and/or 对 flux 的理解不是正确的。
提供一些指导将不胜感激。
我觉得你做的很好。你走在正确的道路上。
React 为您提供了 API 组合,可以准确处理您不确定如何实现的内容 (way to pass props to components rendered by this.props.children
)
首先,你需要看一下cloneElement
它基本上会采用一个 React 元素,克隆它,然后 return 另一个带有道具的元素,您可以完全根据需要更改、更改或替换。
此外,将它与 Children Utilities 结合 - 遍历提供给顶级组件的子元素,并对每个元素分别进行必要的更改。
提议的示例用法可以像
一样简单
<div className='main-content'>
{React.children.map(this.props.children, (child, index) => {
//Get props of child
const childProps = child.props;
//do whatever else you need, create some new props, change existing ones
//store them in variables
return React.cloneElement(child, {
...childProps, //these are the old props if you don't want them changed
...someNewProps,
someOldPropOverwritten, //overwrite some old the old props
});
)}
</div>
使用这些工具随时随地创建真正通用和可重复使用的组件。 Children
中更常用的实用程序是 map
、forEach
和 toArray
。每个人都有自己的目标。
希望对您有所帮助。
这更多是关于 React 的体系结构问题,而不是特定问题,但被认为是管理 state/props 布局组件和基于 [=39] 呈现的多个子组件的最佳实践=]?
注意:我知道有人问过类似的问题,但这有点不同。 [How to update ReactJS component based on URL / path with React-Router
假设我有类似以下代码的内容:一个配置文件页面(主布局视图),其中包含配置文件子部分(设置、首选项、帐户详细信息等)的导航链接,以及一个主面板,其中每个子部分被呈现。
所以目前我会有这样的东西: 我的路由器 routes.js
<Router history={browserHistory}>
<Route path='/profile' component={Profile} >
<IndexRoute component={Summary} />
<Route path='/profile/settings' component={Settings} />
<Route path='/profile/account' component={Account} />
<Route path='/profile/preferences' component={Preferences} />
</Route>
</Router>
和我的个人资料布局组件的精简版 profile.js
class Profile extends React.Component {
constructor(props) {
super(props)
}
render(){
let pathName = this.props.location.pathname;
return(
<div className='container profile-page'>
<div className='side-nav'>
<ul>
<li><Link to='/profile'>Summary</Link></li>
<li><Link to='/profile/settings'>Settings</Link></li>
<li><Link to='/profile/account'>My Account</Link></li>
<li><Link to='/profile/preferences'>Preferences</Link></li>
</ul>
</div>
<div className='main-content'>
{this.props.children}
</div>
</div>
)
}
}
export default Profile;
所以这种作品。子组件将基于 url 进行渲染。但是我该如何管理状态和道具呢?按照我对 React 和 Flux 的理解,我希望 Profile 组件管理状态并监听我的商店的变化,并将这些变化传播给它的子组件。这个对吗?
我的问题是,似乎没有一种直观的方式将 props 传递给 this.props.children 渲染的组件,这让我觉得我当前的架构 and/or 对 flux 的理解不是正确的。
提供一些指导将不胜感激。
我觉得你做的很好。你走在正确的道路上。
React 为您提供了 API 组合,可以准确处理您不确定如何实现的内容 (way to pass props to components rendered by this.props.children
)
首先,你需要看一下cloneElement
它基本上会采用一个 React 元素,克隆它,然后 return 另一个带有道具的元素,您可以完全根据需要更改、更改或替换。
此外,将它与 Children Utilities 结合 - 遍历提供给顶级组件的子元素,并对每个元素分别进行必要的更改。
提议的示例用法可以像
一样简单<div className='main-content'>
{React.children.map(this.props.children, (child, index) => {
//Get props of child
const childProps = child.props;
//do whatever else you need, create some new props, change existing ones
//store them in variables
return React.cloneElement(child, {
...childProps, //these are the old props if you don't want them changed
...someNewProps,
someOldPropOverwritten, //overwrite some old the old props
});
)}
</div>
使用这些工具随时随地创建真正通用和可重复使用的组件。 Children
中更常用的实用程序是 map
、forEach
和 toArray
。每个人都有自己的目标。
希望对您有所帮助。