首页 > 解决方案 > 未能将 {history} 对象作为道具传递给路由器

问题描述

尝试将历史记录对象作为道具传递到我的路由器时出现以下错误。我刚刚将 history={history} 行添加到我的路由器中,出于某种原因它认为它不是传入的对象?

index.js:1446 警告:道具类型失败:提供给history的道具类型无效,应为。functionRouterobject

历史.js

import { createBrowserHistory } from 'history';
export default createBrowserHistory;

应用程序.js

import React from 'react';
import { Router, Route } from 'react-router-dom';
import PostCreate from './posts/PostCreate';
import PostList from './posts/PostList';
import Header from './Header';
import history from '../history';

const App = () => {
    return (
        <div className="ui container">

            <Router history={history} >
                <div>
                    <Header />
                    <Route path="/" exact component={PostList} />
                    <Route path="/posts/new" exact component={PostCreate} />
                </div>
            </Router>
        </div>
    );
};

export default App;

标签: reactjsreact-router

解决方案


我修好了它。在我的 history.js 中,我的 createBrowserHistory 末尾没有 ()。

历史.js

import { createBrowserHistory } from 'history';
export default createBrowserHistory();

推荐阅读