【问题标题】:How to get id/params to redux action from react Router?如何从反应路由器获取 id/params 到 redux 操作?
【发布时间】:2018-08-19 04:42:58
【问题描述】:

目前我有一个对象列表页面。我想显示单个对象的详细信息页面(一切都是用 Reactjs 完成的)。列表对象有一个这样的链接:

<Link to={'/detail/'+this.props.data.id} >Read more</Link>  

单击链接时,它指向另一个页面,我想在其中显示该特定对象的所有详细信息。该特定对象的详细信息从http://127.0.0.1:8000/api/v1/songs/55 获取,其中 55 是对象的 id。我正在使用 Redux 从 API 获取数据

ListingPage.js

import React from 'react';
import { BrowserRouter as Router, Switch, Route, Link } from 'react-router-dom'

class List extends React.Component {
    render() {
      return (
         <div>
            <Link to={'/detail/'+this.props.data.id} >Read more</Link>                      
        </div>
      );
   }
}
export default List

Home.js

import React from 'react'; 
import { BrowserRouter as Router, Switch, Route, Link } from 'react-router-dom'; 
import List from './ListPage.js'; 
import Detail from './Detail.js'

class Home extends React.Component {    
     render(){      
       return(
            <Router>
              <Switch>
                <Route exact path="/" component={List} />
                <Route path="/detail/:number" component={Detail} />
              </Switch>
            </Router>
        )
    }
 }
 export default Home

Detail.js

import React from 'react';

class Detail extends React.Component {
    render() {
        return (
           <div>{**display details here**}</div>
        )
    }
} 
export default Detail

当前点击链接,它指向另一个页面,但没有显示详细信息。我不知道如何从 react-redux 中的 Link url 获取 id。请帮我。 (说真的我必须使用 Redux)

【问题讨论】:

  • 所以你想从&lt;Link to={'/detail/'+this.props.data.id} 得到id 吗?并将其传递给 react-redux ,以管理状态?
  • 是的,单击链接时,当前页面加载时没有任何数据。但我希望该页面有数据。 (与任何列表和详细信息页面功能一样)
  • 可以发details组件代码吗?
  • 从 API 中,我会得到名称、描述等。没有什么比这更复杂的了。
  • 我的理解是您想获取该 ID 并将其传递给 API 以发出请求并显示名称、描述等?

标签: reactjs redux react-router react-redux


【解决方案1】:

您可以使用mapStateToProps 读取由react-router 传递给您的组件的路由参数。您的 :number 命名参数将在 ownProps.match.params.number 可用,您需要将其传递到您的组件中。

然后,您的组件会将其传递给您的操作创建者以加载数据。

更多细节在 cmets 中:

import React from 'react';
import { connect } from "react-redux";

class Detail extends React.Component {
    componentWillMount() {
        // When the component mounts 
        // call your action creator to load the details, 
        // if songDetails are not already available
        if (!this.props.songDetails) 
            this.props.loadSongDetails(this.props.songId);
    }

    render() {
        // Don't show anything until songDetails are loaded
        // Optionally, you can also show a loading screen for better user experience
        if (!this.props.songDetails) return null;

        // Once songDetails are loaded, you can use `this.props.songDetails` to populate your UI
        return (
            <div>{this.props.songDetails}</div>
        )
    }
}

/**
 * 'mapStateToProps' gets a second parameter which contains the props passed directly to the component
 * In this case, react-router will pass route parameters to it.
 * Route parameters will include your named parameter ":number" at "match.params.number".
 * You can pass this parameter into your component, and later pass it into your loading function.
 *
 * "getSongDetails" is a selector function which gets the songDetails from your redux store.
 */
const mapStateToProps = (state, ownProps) => ({
    songId: ownProps.match.params.number,
    songDetails: getSongDetails(state)
});

/**
 * Use 'mapDispatchToProps' to pass your action creator into your component.
 * This action creator will be called with songId when the component loads.
 */
const mapDispatchToProps = (dispatch) => ({
    loadSongDetails: (songId) => dispatch(loadSongDetailsActionCreator(songId))
});

/**
 * Use Redux's "connect" HOC to pass props into your component.
 */
export default connect(mapStateToProps, mapDispatchToProps)(Detail);

【讨论】:

  • 你能显示getSongDetails和loadSongDetailsActionCreator的代码吗?
  • 该实现将特定于您的应用程序。 getSongDetails 只是读取状态并返回歌曲详细信息。您还将 ID 传递给函数,除非它应该从状态本身获取 ID。 loadSongDetailsActionCreator 只需对服务器进行 API 调用以加载相关数据。希望这会有所帮助。
猜你喜欢
  • 1970-01-01
  • 2018-01-14
  • 1970-01-01
  • 2017-03-23
  • 2018-06-13
  • 2018-09-04
  • 2016-06-12
  • 2016-08-24
  • 1970-01-01
相关资源
最近更新 更多