【问题标题】:How can I obtain class props from url and store in react-redux如何从 url 获取类道具并存储在 react-redux
【发布时间】:2020-05-30 23:07:16
【问题描述】:

所以我试图使用路由将参数传递给反应组件,同时也使用组件类道具。这是我在做什么

import { loadSchemes, } from '../../actions/schemes;

export class Schemes extends Component {
constructor(props) {
    super(props);
    const { match: { params } } = this.props;
    this.state = {
      client_id: params.pk,
    }
  }
componentDidMount() {
    this.props.loadSchemes();
  }
render(){
 return(
   <div>
    {this.props.schemes_list.map((scheme,index)=><p key={index}>{scheme}</p>)}
   </div>
  )
 }
}
const mapStateToProps = (state) => ({
  schemes_list: state.schemes,
});
export default connect(mapStateToProps,{ loadSchemes,})(Schemes);

我有一个指向这个组件的 url 作为

<Route path="/client/:pk/schemes" component={Schemes}/>

问题是我得到一个错误 this.props.schemes_list is undefined 并且 this.props.loadSchemes is undefined

请帮助我使用 react-redux

【问题讨论】:

  • import { loadScheme, } 是单数,你在哪里传递“schemes_list”我看不到
  • 导入是一个错字,它们都是 loadSchemes,让我编辑问题,schemes_list 也来自 scheme reducer。

标签: reactjs react-redux


【解决方案1】:

显然,在您调用 Scheme 的组件中,您导入 { Schemes },一个未连接的组件,而不是 Schemes - 默认连接的组件。请检查一下。

【讨论】:

  • 非常感谢,这正是我所缺少的,现在完美运行。
猜你喜欢
  • 2017-12-19
  • 1970-01-01
  • 1970-01-01
  • 2018-04-17
  • 2020-01-29
  • 1970-01-01
  • 2020-08-26
  • 2019-05-17
  • 1970-01-01
相关资源
最近更新 更多