简体   繁体   English

React Redux 路由器传递道具

[英]React Redux Router pass props

i've this App.js我有这个 App.js

const mapStateToProps = (state) => {
 return {
   isPeddingHome: state.getShowsHome.isPeddingHome,
   data: state.getShowsHome.data,
   isPeddingMovies: state.getShowsMovies.isPeddingMovies,
   movies: state.getShowsMovies.movies,
   isPeddingSeries: state.getShowsTv.isPeddingSeries,
   series: state.getShowsTv.series
 }
}

const mapDispatchToProps = (dispatch) => {
 return {
  onGetShows: () => dispatch(getShows()),
  onGetMovies: () => dispatch(getMovies()),
  onGetSeries: () => dispatch(getTvSeries())
 }
}

const App = () =>  {
 return (
  <Router>
    <Switch>
      <Route exact path="/"  component={Home}/>
      <Route path="/movies" component={MoviesPage}/>
      <Route path="/tv" component={tvSeriesPage}/>
     </Switch>
   </Router>
 );
}


export default connect(mapStateToProps, mapDispatchToProps)(App);

i want to pass props from that i create with mapStateToProps, example我想传递我用 mapStateToProps 创建的道具,例如

on Home pass only data, isPeddingHome and onGetShows, it's posssible?在 Home 上只传递数据,isPeddingHome 和 onGetShows,这可能吗?

i try to use我尝试使用

render={(props) => <Home {...props} />}

to pass but not pass nothing to component传递但不传递任何东西给组件

The argument passed to the render prop function are the route props , not the props given to the App component.传递给render prop 函数的参数是route props ,而不是提供给App组件的 props。

You can spread the App props and the route props as well to get what you want.您也可以传播App道具和路线道具以获得您想要的。

const App = props => {
  return (
    <Router>
      <Switch>
        <Route
          exact
          path="/"
          render={routeProps => <Home {...props} {...routeProps} />}
        />
        <Route path="/movies" component={MoviesPage} />
        <Route path="/tv" component={tvSeriesPage} />
      </Switch>
    </Router>
  );
};

The props argument to render prop callback is the router props and no the component props, you need to change your code to pass the props to App component to Home component.渲染道具回调的道具参数是路由器道具而不是组件道具,您需要更改代码以将道具传递给App组件到Home组件。

const App = (props) =>  {
 return (
  <Router>
    <Switch>
      <Route exact path="/"  render={(routerProps) => <Home data={props.data} onGetShows={props.onGetShows} isPeddingMovies={props. isPeddingMovies} {...routerProps}/>}/>
      <Route path="/movies" component={MoviesPage}/>
      <Route path="/tv" component={tvSeriesPage}/>
     </Switch>
   </Router>
 );
}

You can choose to destructure the props in App.js and pass them on to Home like您可以选择在 App.js 中解构 props 并将它们传递给 Home 之类的

const App = ({isPeddingMovies, onGetShows, data}) =>  {
 return (
  <Router>
    <Switch>
      <Route exact path="/"  render={(routerProps) => <Home data={data} onGetShows={onGetShows} isPeddingMovies={isPeddingMovies} {...routerProps}/>}/>
      <Route path="/movies" component={MoviesPage}/>
      <Route path="/tv" component={tvSeriesPage}/>
     </Switch>
   </Router>
 );
}

However since the App component is not consuming all the props from mapStateToProps or mapDispatchToProps you can simply connect the Home component但是,由于 App 组件没有消耗mapStateToPropsmapDispatchToProps所有道具,您可以简单地连接 Home 组件

const mapStateToProps = (state) => {
 return {
   isPeddingHome: state.getShowsHome.isPeddingHome,
   data: state.getShowsHome.data,
   isPeddingMovies: state.getShowsMovies.isPeddingMovies,
 }
}

export default connect(mapStateToProps)(Home);

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

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