简体   繁体   English

如何将props传递给react-router 1.0组件?

[英]How to pass props to react-router 1.0 components?

Please note, that although the question itself is largely a duplicate of this , it concerns a different version which should support this. 请注意,尽管问题本身在很大程度上是对此的重复,但它涉及的另一版本支持此问题。 The linked question already accepted an answer on an old version 链接的问题已接受旧版本的答案

I'm pretty confused about what the intended workflow is. 我对预期的工作流程很困惑。

Let's say I have a menu system where clicking on each item uses react-router to navigate to an area which pulls some data from the server. 假设我有一个菜单系统,其中单击每个项目都使用react-router导航到从服务器提取一些数据的区域。

url: yoursite/#/lists/countries
----------------------------
Billing Codes | <<Countries>> | Inventory Types 
---------------------------------------------------------
Countries:
---------------
Afghanistan
Azerbaijan
Belarus

with routes something like 与类似的路线

Route #/lists component: Lists
   Route billing-codes component: BillingCodes
   Route countries component: Countries
   Route inventory-types component: InventoryTypes

I don't want to preload data from the server until an area is navigated to, so in my Countries component's on componentWillMount I fire an event (I'm using reflux but...whatever) that triggers a store to do an ajax request and update itself with the current list of countries. 我不想从服务器预加载数据,直到导航到某个区域,所以在我的“ Countries组件的componentWillMount我触发一个事件(我正在使用回流,但无论如何),该事件触发商店执行ajax请求并使用当前国家列表进行更新。

Now the Countries component reacts to that change in state by updating the countries in its props. 现在,“ Countries组件通过更新道具中的国家来对状态变化做出反应。 Except - reasonably - that generates an invariant error because I shouldn't be updating props on a child component, I should update it at the top level. 除了-因为我不应该在子组件上更新props-会产生不变错误之外,我应该在顶层进行更新。 But the top level is the router itself so now I'm just lost - where am I supposed to listen to changes and update props from? 但是,顶层是路由器本身所以现在我只是失去了-在应该听取的变化和更新的道具?

( Cross-posted to the issue tracker as I think it needs some clearer documentation) (由于我认为它需要一些更清晰的文档,因此被交叉发布到问题跟踪器中)

Reading the react-router 0.13 -> 1.0 Upgrade Guide and this example led me to the following: 阅读react-router 0.13-> 1.0 Upgrade Guide本示例使我了解以下内容:

{ this.props.children && 
     React.cloneElement(this.props.children, {newprop: this.state.someobject }) }

Instead of including the child components directly, we clone them and inject the new properties. 与其直接包含子组件,不如克隆它们并注入新属性。 (The guard handles the case where there is no child component to be rendered.) Now, when the child component renders, it can access the needed property at this.props.newprop . (守护程序处理没有​​要渲染的子组件的情况。)现在,当子组件渲染时,它可以在this.props.newprop处访问所需的属性。

The easy way is to just use this.state , but if you absolutely have to use this.props then you should probably extend Router.createElement . 简单的方法是只使用this.state ,但是如果您绝对必须使用this.props则应该扩展Router.createElement

First add the createElement prop to your Router render. 首先,将createElement添加到Router渲染中。

React.render(
  <Router history={history} children={Routes} createElement={createElement} />,
  document.getElementById('app')
);

Wrap all of your components in a new Container component. 将所有组件包装在新的Container组件中。

function createElement(Component, props) {
    return <Container component={Component} routerProps={props} />;
}

Your Container component will probably look something like this. 您的Container组件可能看起来像这样。 Pass an onLoadData function down to your component. 向下传递一个onLoadData函数到您的组件。

import React from 'react';
import _ from 'lodash';

class Container extends React.Component {
  constructor(props) {
    super(props);
    this.state = { props: props.routerProps };
  }

  onLoadData(props) {
    var mergedProps = _.merge(this.state.props, props);
    this.setState({ props: mergedProps });
  }

  render() {
    var Component = this.props.component;
    return <Component {...this.state.props} onLoadData={this.onLoadData.bind(this)} />;
  }
}

Then from your loaded component, when you get your data back from the server, just fire this.props.onLoadData(data) and the data will be merged with your current props. 然后从加载的组件中,当您从服务器this.props.onLoadData(data) ,只需触发this.props.onLoadData(data) ,数据便会与当前的props合并。

Read the Router.createElement Documentation 阅读Router.createElement文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM