简体   繁体   English

React.js客户端路由匹配,给出“提供给`Router`的无效道具`routes。”

[英]React.js client route match giving “Invalid prop `routes` supplied to `Router`.”

Compiling my packages with webpack (there is a separate vendor.js and then an app.js, vendor.js just requires some basics like react, etc). 使用webpack编译我的软件包(有一个单独的vendor.js,然后是app.js,vendor.js仅需要一些基本知识,例如react等)。 I'm wondering why when I use {...renderProps} format in the Router declaration, that it throws this error. 我想知道为什么在路由器声明中使用{... renderProps}格式时会引发此错误。 If I change {...renderProps} to the standard routes={routes} format, it seems to work, but everyone tends to use {...renderProps}, and I'm trying to figure out why it isn't working for me. 如果我将{... renderProps}更改为标准route = {routes}格式,则似乎可以使用,但是每个人都倾向于使用{... renderProps},因此我试图弄清为什么它不起作用为了我。 I believe the dot alias {...x} is a stage-0 feature, so I have stage-0 defined in my webpack presets, but doesn't seem to affect the outcome. 我相信点别名{... x}是阶段0的功能,因此我在webpack预设中定义了阶段0,但似乎并不影响结果。 Here's my files: 这是我的文件:

app.js: app.js:

import React from 'react';
import { render } from 'react-dom';
import { match, Router, RouterContext, browserHistory } from 'react-router'
import { createHistory } from 'history'
import routes from './components/routes/AppRoute.jsx';

const { pathname, search, hash } = window.location;
const location = pathname;

match({ routes, location }, (error, redirectLocation, renderProps) => {
   render(
        <Router 
            {...renderProps} 
            history={browserHistory} 
        />, 
        document.getElementById('react-app')
    );
})

/components/routes/AppRoute.jsx is: /components/routes/AppRoute.jsx是:

import AppShell from '../AppShell.jsx';
import Index from '../Index.jsx';

if(typeof require.ensure !== "function") require.ensure = function(d, c) { c(require) }

module.exports = {
  path: '/',
  component: AppShell,
  indexRoute: Index,
  childRoutes: [
    { path: 'test', 
        getComponent(location, cb) {
            require.ensure([], (require) => {
              cb(null, require('../Test.jsx'))
            })
          }
    },
    { path: 'user', 
        getComponent(location, cb) {
            require.ensure([], (require) => {
              cb(null, require('../User.jsx'))
            })
          }
    },
    { path: '*', 
        getComponent(location, cb) {
            require.ensure([], (require) => {
              cb(null, require('../NotFound.jsx'))
            })
          }
    },
    { path: 'app-shell', component: AppShell }
  ] 
}

This seems to be exported correctly and being imported correctly into app.js, as console.log shows the object as expected. 这似乎已正确导出,并已正确导入到app.js中,因为console.log按预期显示了该对象。

But for some reason upon page load, the browser gives: 但是出于某种原因,页面加载后,浏览器会给出:

dll.vendor.js:330 Warning: Failed prop type: Invalid prop `routes` supplied to `Router`.
    in Router

This error is being displayed from the dll.vendor.js file (not app.js), but I'm not sure that matters as I react is loaded into the vendors file, and react is responsible for showing the errors. 该错误是从dll.vendor.js文件(不是app.js)显示的,但是我不确定这是很重要的,因为我将React加载到了vendors文件中,而react负责显示错误。

Anyone have an idea as to why? 有人知道为什么吗?

Does changing the match in app.js from <Router> to <RouterContext> and deleting the history prop fix it? app.jsmatch<Router>更改为<RouterContext>并删除历史记录道具是否可以解决问题?

match({ routes, location }, (error, redirectLocation, renderProps) => {
   render(
        <RouterContext 
            {...renderProps} 
        />, 
        document.getElementById('react-app')
    );
})

That's what is recommended in react-router's docs 那是在React-Router的文档中推荐

React router 2 反应路由器2

It is because the Router component does not take routes as a prop. 这是因为Router组件不将routes作为支持。

As this is for server-side rendering, the code need to use RouterContext rather than Router . 因为这是用于服务器端渲染,所以代码需要使用RouterContext而不是Router

React router 4 反应路由器4

match and Router has been removed to make life simpler and been replaced with MemoryRouter , BrowserRouter and ServerRouter . matchRouter被删除以简化生活,并被MemoryRouterBrowserRouterServerRouter取代。

/* eslint-disable no-param-reassign */
import React from 'react';
import { renderToString } from 'react-dom/server';
import { ServerRouter/* , createServerRenderContext */ } from 'react-router';
// todo : remove line when this PR is live
// https://github.com/ReactTraining/react-router/pull/3820
import createServerRenderContext from 'react-router/createServerRenderContext';
import { makeRoutes } from '../../app/routes';

const createMarkup = (req, context) => renderToString(
  <ServerRouter location={req.url} context={context} >
    {makeRoutes()}
  </ServerRouter>
);

const setRouterContext = (req, res, next) => {
  const context = createServerRenderContext();
  const markup = createMarkup(req, context);
  const result = context.getResult();
  if (result.redirect) {
    res.redirect(301, result.redirect.pathname + result.redirect.search);
  } else {
    res.status(result.missed ? 404 : 200);
    res.routerContext = (result.missed) ? createMarkup(req, context) : markup;
    next();
  }
};

export default setRouterContext;

for a working demo take a look at react-lego 工作演示,请看一下react-lego

ah, ok - the confusion was around the use of reactRouter.match and RouterContext both of which I believe are for server-side rendering only. 嗯,好的-混乱之处在于对reactRouter.matchRouterContext的使用,我认为这两者仅用于服务器端渲染。

You should have a Router , which takes children and a history (browser or memory). 您应该有一个Router ,它带有孩子和一个历史记录(浏览器或内存)。

ReactDOM.render(
    <Router history={history} >
        <Route path="/" component={ MainLayout }>
          <IndexRoute { ...indexRoute(routes.homepage) } />
          <Route { ...routes.game } />
          <Route { ...routes.notFound } />
        </Route>
    </Router>,
document.getElementById('html'));

暂无
暂无

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

相关问题 警告:道具类型失败:提供给“路线”的道具属性无效。 &警告:[反应路由器]位置“ /”与任何路线均不匹配 - Warning: Failed prop type: Invalid prop `component` supplied to `Route`. & Warning: [react-router] Location “/” did not match any routes 在react.js中使用route时,为route提供了无效的prop`component` - Invalid prop `component` supplied to `Route` while using route in react.js React Router-组成路线时提供给`Router`的无效道具`children&#39; - React Router - Invalid prop `children` supplied to `Router` when composing routes 提供给“GoogleMap”的道具“center”无效。 React.js - Invalid prop `center` supplied to `GoogleMap`. React.js 反应路由器错误:无效的道具组件提供给路线 - React Router Error: Invalid prop `component` supplied to `Route` React-Router:失败的prop类型:在route中提供给Route的无效prop`component` - React-Router: Failed prop type: Invalid prop `component` supplied to `Route` in route 反应路由器,多个组件警告:道具类型失败:提供给“路线”的道具“组件”无效 - react router, multiple components warning: Failed prop type: Invalid prop `component` supplied to `Route` 警告:失败的道具类型:提供给“路由”的无效道具“组件”-react-router-dom - Warning: Failed prop type: Invalid prop 'component' supplied to 'Route' - react-router-dom 带有React 16.6 Suspense的React Router“向“ Route”提供了类型为“ object”的无效prop`component`,预期为“ function”。” - React Router with React 16.6 Suspense “Invalid prop `component` of type `object` supplied to `Route`, expected `function`.” 警告:失败的道具类型:提供给“路由”的“对象”类型的无效道具“组件”,预期的“函数”使用 react-router-dom 错误 - Warning: Failed prop type: Invalid prop `component` of type `object` supplied to `Route`, expected `function` Error using react-router-dom
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM