【问题标题】:Rendering noMatch in React Router v4 doesn't work?在 React Router v4 中渲染 noMatch 不起作用?
【发布时间】:2017-06-27 04:01:31
【问题描述】:

这是我的代码:

class noMatch extends React.Component {
  render() {
    return 'Not Found'
  }
}
ReactDOM.render(
   <BrowserRouter>
      <Switch>
        <Route exact path='/(index.html)?' component={App}/>
        <Route component={noMatch}/>
      </Switch>
    </BrowserRouter>,
  mountNode
);

应用程序按预期在 localhost:3000/ 和 localhost:3000/index.html 中运行。但是当没有匹配时,我的 no Match 不会渲染?我正在关注文档here 中的 noMatch 教程。帮忙?

【问题讨论】:

  • 检查重复问题,答案将帮助您理解您的错误。如果您仍然无法解决问题,请告诉我
  • 我将我的 NoMatch 组件大写,但它仍然不起作用。
  • 控制台中的任何错误
  • 只是没有找到 404。但我期待 NoMatch 组件呈现

标签: reactjs react-router react-router-v4 react-router-dom


【解决方案1】:

您的路由器代码看起来可以找到。如果你只是在你的 noMatch 组件中返回字符串,你应该返回有效的 React 元素,例如:

class noMatch extends React.Component {
    render() {
        return (
            <div>Not Found</div>
        );
    }
}

【讨论】:

    猜你喜欢
    • 2018-02-10
    • 1970-01-01
    • 2019-01-15
    • 2018-01-10
    • 2018-05-14
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 2018-06-06
    相关资源
    最近更新 更多