简体   繁体   English

在连接到不同状态部分的不同路由上重用react / redux组件

[英]Reusing react/redux component on different routes connected to different part of state

I am running into a problem because I have a fairly complex component that renders a list of items. 我遇到了一个问题因为我有一个相当复杂的组件来呈现项目列表。 The list of items is taken directly from the redux state tree. 项目列表直接从redux状态树中获取。 I want to have 2 separate routes that reuse this same component, but each of them will connect to a different list from the state tree. 我希望有两个单独的路由重用相同的组件,但每个路由都将连接到状态树中的不同列表。

Here is a simplified example: 这是一个简化的例子:

Starting with the state tree: 从状态树开始:

state = {
    fruits: ['apple', 'banana', 'grape', 'pear'],
    vegetables: ['celery', 'carrot', 'cucumber']
}

and a simple listing component 和一个简单的列表组件

class ListView extends Component {
  render() {
    return (
      <div>
        {this.props.items.map((item, index) => 
          <div key={index}>{item}</div>
        )}
      </div>
    )
  }
}

function mapStateToProps(state) {
  return {
    items: state.fruits
  }
}

export default connect(mapStateToProps)(ListView)

The higher level App component looks like this: 更高级别的App组件如下所示:

class App extends Component {
  render() {
    return (
      <div>
        {this.props.children}
      </div>
    )
  }
}

export default connect()(App)

And the routes look like this: 路线看起来像这样:

  <Route path='/' component={App}>
    <IndexRedirect to='fruits' />
    <Route path='fruits' component={ListView} />
    <Route path='vegetables' component={ListView} />
  </Route>

So right now the ListView component is connected to the fruits part of the redux state. 所以现在ListView组件连接到redux状态的fruits部分。 However I would like to utilize the route structure here so '/fruits' and '/vegetables' will both use the same component, but '/fruits' will list the fruits while '/vegetables' will list the vegetables. 但是我想在这里使用路线结构,所以'/ fruits'和'/ vegetables'都会使用相同的组件,但'/ fruits'会列出水果,而'/ vegetables'会列出蔬菜。

The obvious work around is to create a completely new and identical component class and change the mapStateToProps method to connect to the vegetables array. 显而易见的工作是创建一个全新的,相同的组件类,并更改mapStateToProps方法以连接到vegetables数组。

Is there a way to reuse this component but have access to different parts of the state tree in each? 有没有办法重用这个组件,但可以访问每个组件中状态树的不同部分?

From your ListView, you could export both FruitsList and VeggiesList as separate components, then display the correct one based on route. 从ListView中,您可以将FruitsList和VeggiesList导出为单独的组件,然后根据路径显示正确的组件。

class ListView extends Component {
  render() {
    return (
      <div>
        {this.props.items.map((item, index) => 
          <div key={index}>{item}</div>
        )}
      </div>
    )
  }
}

function mapStateToPropsVeggies(state) {
  return {
    items: state.veggies
  }
}

function mapStateToPropsFruits(state) {
  return {
    items: state.fruits
  }
}

const VeggiesList = connect(mapStateToPropsVeggies)(ListView);
const FruitsList = connect(mapStateToPropsFruits)(ListView);

export default ListView;
export {
    VeggiesList,
    FruitsList
};

Then update the router to: 然后将路由器更新为:

<Route path='/' component={App}>
  <IndexRedirect to='fruits' />
  <Route path='fruits' component={FruitsList} />
  <Route path='vegetables' component={VeggiesList} />
</Route>

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

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