简体   繁体   English

在反应路由器中的孩子之间传递数据

[英]passing data between children in react-router

I have a problem.我有个问题。 The problem is I need somehow to get product id from the ReadProducts component and pass it to ReadOne components, because I can't get an id in function and can't show the product.问题是我需要以某种方式从 ReadProducts 组件中获取产品 id 并将其传递给 ReadOne 组件,因为我无法在函数中获取 id 并且无法显示产品。 How can I do this?我怎样才能做到这一点?

The structure is below:结构如下:

This component is parent for products, to get all and to get one此组件是产品的父级,获取所有并获取一个

  class Product extends Component {
      render() {
        return (
          <Switch>
            <Route path='/products' component={ReadProducts} />
            <Route path='/products/:id' component={ReadOne} />
          </Switch>
        )
      }
    };

This one is getting all the products from api这是从 api 获取所有产品

   class ReadProducts extends Component {
      constructor(props) {
        super(props);
        this.state = {
          products: []
        }
        this.getProduct = this.getProduct.bind(this);
      }

      render() {
        const { products } = this.state;

        return(
            <ul>
              {
                products.map(product => (
                  <li key={product.id}>
                    <Link to={`/products/${product.id}`}><button onClick={???}>{product.name}</button></Link>
                  </li>
                ))
              }
            </ul>
        )
      }

    }

This is for reading one product这是用于阅读一种产品

class ReadOne extends Component {
  constructor(props) {
    super(props);
    this.state = {
      id: 0,
      ...
    }
  }

  render() {
    return(
      <div className='pew'>
        {this.state.name}, {this.state.id}...
      </div>
    )
  }
}

The problem is I need somehow to get product id from the ReadProducts component and pass it to ReadOne components, because I can't get an id in function and can't show the product.问题是我需要以某种方式从 ReadProducts 组件中获取产品 id 并将其传递给 ReadOne 组件,因为我无法在函数中获取 id 并且无法显示产品。 How can I do this?我怎样才能做到这一点?

Add this in Product.js在 Product.js 中添加这个

We set the initial state of id.我们设置id的初始状态。

We add setProductId in Product.js because this is where the state of id has to be in order for you to use it in ReadOne.js.我们在 Product.js 中添加 setProductId ,因为这是 id 的状态,以便您在 ReadOne.js 中使用它。

  constructor(props) {
   super(props);
    this.state={
      id:' ',
    }
}
setProductId(id) => {
  this.setState({ id:id })
}

Then pass id down as a prop to ReadOne component.然后将 id 作为道具传递给 ReadOne 组件。

  <Route path='/products/:id' render={()=> <ReadOne id={this.state.id}/>}>

This is the getter for the id, add this inside of ReadProducts.js这是 id 的 getter,将其添加到 ReadProducts.js 中

showProduct = (id) => {
  getProduct(id);
}

<Link to={`/products/${product.id}`}><button onClick={this.showProduct(product.id)}>{product.name}</button></Link>

Now you can use id in any component within Product.js现在您可以在 Product.js 中的任何组件中使用 id

Quick note from the react-router docs.来自 react-router 文档的快速说明。

When you use component (instead of render or children, below) the router uses React.createElement to create a new React element from the given component.当您使用组件(而不是渲染或子组件,如下所示)时,路由器使用 React.createElement 从给定组件创建一个新的 React 元素。 That means if you provide an inline function to the component prop, you would create a new component every render.这意味着如果您为组件 prop 提供内联函数,您将在每次渲染时创建一个新组件。 This results in the existing component unmounting and the new component mounting instead of just updating the existing component.这会导致卸载现有组件并安装新组件,而不仅仅是更新现有组件。

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

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