微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

反应路由器不渲染组件然后他有参数

如何解决反应路由器不渲染组件然后他有参数

大家好!

我怎么了?

我正在尝试制作一个产品详细信息页面,然后我正在使用它:

 return (
   <Router>
      <div>
         <Header user={this.state.user} setUser={this.setUser} />
      </div>
       <div className="mx-10">
          <Switch>
            <Route exact path="/details/:slug" components={() => <Details />} />
            <Route exact path="/market/search/:categorie" component={DynamicSearchCategory} />
         </Switch>
       </div>
   </Router>
);

这真的很好/market/search/:categorie

但是,如果我要获取页面/details/:slug

我也尝试过以下方法/market/details/:slug

如果我获得产品详细信息路线,则什么也不会显示,但是出现标题,但是我要渲染的组件functional componentsclass components则什么也不会显示

如果我这样做:

console.log(props)

// or

console.log(this.props) // for class components

控制台上不会显示任何错误

我不知道我是否有任何遗漏,我也不明白为什么/market/search/:categorie工作得很好,但是/details/:slug却不行

有一部分我会向他们提供道具:

class components

import React from 'react'

export default class Details extends React.Component {
    constructor(props) {
        super(props);
    }

    componentDidMount() {
        console.log(this.props)
    }

    render() {
        return (
            <p>Details</p>
        )
    }
}

或在功能组件中:

import React from 'react'

export default function ProductDetails(props) {
    console.log(props.match.params.slug)
    return (
        <>
            <p>Product Details</p>
        </>
    )
}

编辑:

在我的导航器中出现了sl。

解决方法

<Route exact path="/details/:slug" components={() => <Details />} />

=>

<Route exact path="/details/:slug" component={Details}/>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。