【问题标题】:Failing to pass {history} object as a prop into Router未能将 {history} 对象作为道具传递给路由器
【发布时间】:2019-09-04 01:51:56
【问题描述】:

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

index.js:1446 警告:失败的道具类型:提供给function 类型的无效道具history Router,预期为object

history.js

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

App.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;

【问题讨论】:

    标签: reactjs react-router


    【解决方案1】:

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

    history.js

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

    【讨论】:

      猜你喜欢
      • 2018-11-03
      • 1970-01-01
      • 2021-07-12
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多