Reactjs 如何通过路由器传递状态?
这是给我带来麻烦的文件:Reactjs 如何通过路由器传递状态?,reactjs,react-router,browser-history,Reactjs,React Router,Browser History,这是给我带来麻烦的文件: var Routers = React.createClass({ getInitialState: function(){ return{ userName: "", relatives: [] } }, userLoggedIn: function(userName, relatives){ this.setState({ userName: userName, relative
var Routers = React.createClass({
getInitialState: function(){
return{
userName: "",
relatives: []
}
},
userLoggedIn: function(userName, relatives){
this.setState({
userName: userName,
relatives: relatives,
})
},
render: function() {
return (
<Router history={browserHistory}>
<Route path="/" userLoggedIn={this.userLoggedIn} component={LogIn}/>
<Route path="feed" relatives={this.state.relatives} userName={this.state.userName} component={Feed}/>
</Router>
);
}
});
或者,如果您通过链接执行此操作:
<Link
to={{
pathname: '/pathname',
state: { message: 'hello, im a passed message!' }
}}/>
我希望这有帮助!:) 一旦安装路由器组件,就不能更改React路由器的状态。您可以编写自己的HTML5路由组件并侦听url更改
class MyRouter extend React.component {
constructor(props,context){
super(props,context);
this._registerHashEvent = this._registerHashEvent.bind(this)
}
_registerHashEvent() {
window.addEventListener("hashchange", this._hashHandler.bind(this), false);
}
...
...
render(){
return ( <div> <RouteComponent /> </div>)
}
类MyRouter extend.component{
构造函数(道具、上下文){
超级(道具、背景);
this.\u registerHashEvent=this.\u registerHashEvent.bind(this)
}
_registerHashEvent(){
window.addEventListener(“hashchange”,this.\u hashHandler.bind(this),false);
}
...
...
render(){
返回()
}
tl;dr在管理需要在整个应用程序中访问的状态时,最好使用类似于redux
或mobx
的存储。这些库允许组件连接到/观察状态,并随时更新任何状态更改
什么是
?
无法通过
组件传递道具的原因是它们不是真正的组件,因为它们不呈现任何内容。相反,它们用于构建路由配置对象
这意味着:
<Router history={browserHistory}>
<Route path='/' component={App}>
<Route path='foo' component={Foo} />
</Route>
</Router>
我对mobx
不是特别熟悉,但据我所知,设置它更容易。使用redux
、mobx
或其他状态管理之一是在整个应用程序中传递状态的好方法
注意:您可以停止阅读此处。下面是传递状态的合理示例,但您可能只需要使用存储库
没有商店
如果你不想使用商店怎么办?你运气不好吗?不,但是你必须使用React:the的实验性功能。为了使用上下文
,你的父组件之一必须显式定义getChildContext
方法以及childContextTypes
对象。任何子组件想要通过上下文
访问这些值的t需要定义一个contextTypes
对象(类似于propTypes
)
这当然是乏味的,尤其是当您需要通过多个级别的
组件传递此信息时。您还需要管理基本
组件中的状态(即
)因为您没有办法从
的父组件注入状态,所以我知道回答这个问题可能已经太晚了,但是对于将来想知道这一点的任何人来说,您可以这样做:
export default class Routes extends Component {
constructor(props) {
super(props);
this.state = { config: 'http://localhost' };
}
render() {
return (
<div>
<BrowserRouter>
<Switch>
<Route path="/" exact component={App} />
<Route path="/lectures" exact render={() => <Lectures config={this.state.config} />} />
</Switch>
</BrowserRouter>
</div>
);
}
导出默认类路由扩展组件{
建造师(道具){
超级(道具);
this.state={config:'http://localhost' };
}
render(){
返回(
} />
);
}
}
通过这种方式,您可以访问演讲组件中的配置道具。
这是一个关于这个问题的小讨论,但首先它是一个很好的接触!) 对于像我这样的人
通常也可以渲染以下内容:
import {
BrowserRouter as Router,
Router,
Link,
Switch
} from 'react-router-dom'
<Router>
<Switch>
<Link to='profile'>Profile</Link>
<Route path='profile'>
<Profile data={this.state.username} />
</Route>
<Route component={PageNotFound} />
</Switch>
</Router>
导入{
BrowserRouter作为路由器,
路由器,
链接
转换
}从“反应路由器dom”
轮廓
这对我有用 如果您使用的是查询字符串
,则需要添加搜索
例如:
{
key: 'ac3df4', // not with HashHistory!
pathname: '/somewhere',
search: '?some=search-string',
hash: '#howdy',
state: {
[userDefined]: true
}
}
花了大约20分钟的时间才弄清楚为什么我的路线没有被呈现出来。请检查这个答案。
有一个旧版本,所以您将继续使用组件
道具,而不是回答所使用的处理程序。将写一个带有解释的答案。不,不,不!我已经找到了解决办法!我在找到解决方案后立即删除了我的评论,但不幸的是,你似乎已经将其删除了。不过非常感谢:)@PaulS经过再三思考,我一直在努力学习,我找到的解决方案似乎只是一个半解决方案。你介意向我解释一下你提到的解决方案在我的场景中是什么样子吗?太好了!:)请将更新后的解决方案保留为答案,然后将答案标记为正确答案。问题本身正在更改路由状态。请注意,React。自React v15.5以来,PropTypes
已移动到不同的包中。请改用“prop type”库:那么为什么LocationDescriptorObject接口中有.state属性呢?这令人困惑-我在文档中找不到任何东西。。。我的意思是,无论如何我都在使用redux,但它的用意是什么?@YannicHamann location.state来自浏览器(history.pushState/replaceState的第一个参数是可序列化的状态,与location关联),并且附带了一些警告,比如当您直接导航到页面时,它为null。我发现location.state最适合用于临时数据,比如登录后导航到的URL,“真实”状态最好保存在其他位置。我认为这并不能真正回答最初的问题。这如何提供将路线与州连接的能力?
function withProps(Component, props) {
return function(matchProps) {
return <Component {...props} {...matchProps} />
}
}
class MyApp extends React.Component {
render() {
return (
<Router history={browserHistory}>
<Route path='/' component={App}>
<Route path='foo' component={withProps(Foo, { test: 'ing' })} />
</Route>
</Router>
)
}
}
const Foo = (props) => (
<div>{props.username}</div>
)
function mapStateToProps(state) {
return {
value: state.username
};
}
export default connect(mapStateToProps)(Foo)
class MyApp extends React.Component {
getChildContext() {
return {
username: this.state.username
}
}
}
MyApp.childContextTypes = {
username: React.PropTypes.object
}
const Foo = (props, context) => (
<div>{context.username}</div>
)
Foo.contextTypes = {
username: React.PropTypes.object
}
const Bar = (props) => (
<div>These are my props: {JSON.stringify(props)}</div>
)
const Foo = (props) => (
<div>
This is my child: {
props.children && React.cloneElement(props.children, { username: props.username })
}
</div>
)
export default class Routes extends Component {
constructor(props) {
super(props);
this.state = { config: 'http://localhost' };
}
render() {
return (
<div>
<BrowserRouter>
<Switch>
<Route path="/" exact component={App} />
<Route path="/lectures" exact render={() => <Lectures config={this.state.config} />} />
</Switch>
</BrowserRouter>
</div>
);
}
import {
BrowserRouter as Router,
Router,
Link,
Switch
} from 'react-router-dom'
<Router>
<Switch>
<Link to='profile'>Profile</Link>
<Route path='profile'>
<Profile data={this.state.username} />
</Route>
<Route component={PageNotFound} />
</Switch>
</Router>
{
key: 'ac3df4', // not with HashHistory!
pathname: '/somewhere',
search: '?some=search-string',
hash: '#howdy',
state: {
[userDefined]: true
}
}