【问题标题】:Warning: Failed prop type: Invalid prop `component` of type `object` supplied to `Route`, expected `function`警告:失败的道具类型:提供给“路线”的“对象”类型的无效道具“组件”,预期的“功能”
【发布时间】:2017-04-13 15:56:36
【问题描述】:

这是什么错误???

我得到旧的工作代码安装新版本的库,我看到了那个错误。但我不明白这是什么意思。

//index.js
import React from 'react'
import {render} from 'react-dom'
import { BrowserRouter as Router } from 'react-router-dom'
import {Provider} from 'react-redux'
import configureStore, {history} from './store/configureStore'
import {routes} from './routes'
import { ConnectedRouter, routerMiddleware, push } from 'react-router-redux'

const store = configureStore();

render(
    <Provider store={store}>
        <Router history={history} routes={routes}/>
    </Provider>,
    document.getElementById('app')
);

//routes.js
import React from 'react'
import {Route} from 'react-router-dom'
import App from './containers/App'

export const routes = (
    <div>
        <Route path='/' component={App} />
    </div>
);

//App.js
import React, {Component} from 'react'
import NavLink from '../../components/NavLink'

export default class App extends Component {
    render() {
        return (
            <div className='container'>
                <ul className='nav nav-pills'>
                    <li>
                        <NavLink onlyActiveOnIndex={true} to='/'>Главная</NavLink>
                    </li>
                </ul>
                {this.props.children}
            </div>
        )
    }
}

这是什么错误?我的代码有什么问题? 安装 react-router-dom@^4.1.0:

【问题讨论】:

标签: reactjs react-router-v4


【解决方案1】:

只需强制它成为一个函数,因为 prop 期望摆脱控制台错误,即使它不是理想的解决方案。

<Route path='/' component={props => <App {...props} />} />

https://github.com/ReactTraining/react-router/issues/6420#issuecomment-433541079

【讨论】:

    猜你喜欢
    • 2020-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-22
    • 2020-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多