简体   繁体   English

OwnProps Match和Params未定义React-Router V3

[英]OwnProps Match and Params are undefined React-Router V3

Trying to grab id from my url so that I can use it to fetchData on componentDidMount . 试图抓住id从我的网址,这样我可以用它来fetchData上componentDidMount I'm also using it to set some state in the component to display information on the page. 我也用它在组件中设置一些状态来显示页面上的信息。 However, both ownProps.match and ownProps.params keeps coming through as undefined. 但是, ownProps.matchownProps.params都是未定义的。 I've tried a lot of things and all I can think of is that my / route is matching before thing/:id and it's causing an issue? 我已经尝试了很多东西,我能想到的是我的/ route在事物/:id之前是匹配的并且它导致了一个问题?

(I can use either and they fail like so). (我可以使用其中之一,但它们会失败)。 TypeError: this.props.params is undefined TypeError: this.props.match is undefined TypeError: this.props.params is undefined TypeError: this.props.match is undefined

Here are my routes: 这是我的路线:

export default (
  <Route path="/" component={App}>
    <Route path="login" component={requireNoAuthentication(LoginView)} />
    <Route path="register" component={requireNoAuthentication(RegisterView)} />
    <Route path="home" component={HomeContainer} />
    <Route path="thing/:id" component={ThingContainer} />
    <Route path="category" component={CategoryContainer} />
    <Route path="analytics" component={requireAuthentication(Analytics)} />
    <Route path="basic" component={requireNoAuthentication(BasicContainer)} />
    <Route path="*" component={DetermineAuth(NotFound)} />
  </Route>
);

mapStateToProps here (I've usually been commenting out thing until I can figure out the issue so can ignore that one). mapStateToProps在这里(我经常评论出来的thing直到我能找出问题所以可以忽略那个)。 Where I'm really seeing the issue is the const {id} portion. 我真正看到的问题是const {id}部分。

function mapStateToProps(state, ownProps) {
  console.log(ownProps);
  return {
    data: state.data,
    token: state.auth.token,
    loaded: state.data.loaded,
    isFetching: state.data.isFetching,
    isAuthenticated: state.auth.isAuthenticated,
    thing: state.things[ownProps.match.params.id],
  };
}

Here's where a piece of my component that's giving me the headache. 这是我的组件让我头痛的地方。

 @connect(mapStateToProps, mapDispatchToProps)
    export class Thing extends Component {
      constructor(props) {
        super(props);
      }

      componentDidMount() {
        const { id } = this.props.match.params;
        this.fetchData();
        this.props.fetchThing(id);
      }

Btw, ownProps is coming through just fine when I console.log it above. 顺便说一句,ownProps通过就好了未来,当我console.log它上面。 However it's missing any params/match which made me thing it was the route matching before it's intended match? 然而,它缺少任何参与/匹配,这使得我在预期匹配之前匹配路线? That would be ridiculous though because I couldn't ever do a nested route without losing my react-router params? 这可能是荒谬的,因为我不能做一个嵌套的路线而不会失去我的react-router参数?

I'm to the point now where I've started at it so long that I'm losing focus. 我现在已经到了这个地步,我已经开始了这么长时间以至于我失去了焦点。 Turning to some wonderful person for help! 转向一些很棒的人寻求帮助!

Application is react 15.3, react-router v3, redux. 应用程序是反应15.3,react-router v3,redux。

I see that you r rendering "ThingContainer" but ur class is an "Thing". 我看到你渲染“ThingContainer”,但是你的类是“Thing”。 Where ru using a HOC? ru使用HOC? Did you remember to pass all the props to the child ? 你还记得把所有道具传给孩子吗?

Example: 例:

const ThingContainer = (props) => <Thing ...props />

The props object returned from mapStateToProps() should include ownProps.match if you want to use it in your component. 如果要在组件中使用,则从mapStateToProps()返回的props对象应包含ownProps.match Either add it as another property, or merge ownProps into the object being returned. 将其添加为另一个属性,或将ownProps合并到要返回的对象中。

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

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