简体   繁体   English

react-router 中的嵌套组件未呈现

[英]Nested component in react-router is not rendering

I am just practicing how react-router works.我只是在练习 react-router 的工作原理。 This is my App.js in which see message route这是我的 App.js,在其中查看消息路由

import { BrowserRouter as Router, Switch, Route, Link } from 'react-router-dom';
import logo from './logo.svg';
import './App.css';
import styled, { css } from 'styled-components';
import About from './components/About';
import Home from './components/Home';
import Messages from './components/Messages';


const Ul = styled.ul`
    list-style-type: none;
    padding: 0;
    margin: 0;
    background-color: #282c34;
`;
function App() {
    return (
        <div className='App'>
            <header className='App-header'>
                <img src={logo} className='App-logo' alt='logo' />
                <div>welcome to React</div>
            </header>
            <Ul>
                <Li>
                    <NewLink to='/'>Home</NewLink>
                </Li>
                <Li>
                    <NewLink to='/messages'>Messages</NewLink>
                </Li>
                <Li>
                    <NewLink to='/about'>About</NewLink>
                </Li>
            </Ul>
            <Container>
                <Route exact path='/' component={Home} />
                <Route exact path='/messages' component={Messages} />
                <Route exact path='/about' component={About} />
            </Container>
        </div>
    );
}

export default App;

when route switched to messages route messages get rendered **This is messages Route **当路由切换到消息路由消息得到渲染**这是消息路由**

import React from 'react';
import { Route } from 'react-router-dom';
import { Li, NewLink } from '../App';
import Message from './Message';
const Messages = ({ match }) => (
    <div>
        <ul>
            {[...Array(5).keys()].map((n) => (
                <Li key={n}>
                    <NewLink primary to={`${match.url}/${n + 1}`}>
                        Message {n + 1}
                    </NewLink>
                </Li>
            ))}
        </ul>
        <Route path={`${match.url}/:id`} component={Message} />
    </div>
);

export default Messages;

**This is message route but it is not rendering below Messages Route ** **can anyone please tell what is the problem ** **这是消息路由,但它没有在消息路由下方呈现 ** **任何人都可以告诉我问题是什么 **

import React from 'react';

const Message = ({ match }) => <h3>Message with ID {match.params.id}</h3>;

export default Message;

The issue here is the presence of exact prop on route /messages这里的问题是exact prop on route /messages的存在

Now when you have the exact prop on Route, unless the path matches exactly the Route is not rendered and hence the child Routes will never be evaluated.现在,当您在 Route 上拥有 exact 道具时,除非路径完全匹配,否则不会呈现 Route,因此永远不会评估子 Routes。

A better practise is to use Switch component which renders the first matching Route and re-order the Routes so that prefix paths are the end更好的做法是使用Switch组件,它呈现第一个匹配的路由并重新排序路由,以便前缀路径结束

<Switch>
    <Route path='/messages' component={Messages} />  // no exact props
    <Route path='/about' component={About} /> 
    <Route path='/' component={Home} /> // home route rendered at the end
</Switch>

Also on a sidenote, in order to define paths on Routes, you must use match.path instead of match.url同样在旁注中,为了在路由上定义路径,您必须使用match.path而不是match.url

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

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