reactjs - 不变失败 : You should not use <withRouter(Sidebar)/> outside a <Router>

标签 reactjs typescript react-router react-router-dom

我正在尝试创建reactjs组件并在另一个tsx文件中使用,但出现以下错误 不变失败:您不应该在外部使用

我的代码如下,我的codesandbox https://codesandbox.io/s/zen-sound-ztbjl

class Sidebar extends Component<ISidebarProps & RouteComponentProps<{}>> {
    constructor(props: ISidebarProps & RouteComponentProps<{}>) {
        super(props)
        this.state = {}
    }

    componentDidMount = (): void => {
        this.initMenu()
    }

    componentDidUpdate = (prevProps: any): void => {
        if (this.props.type !== prevProps.type) {
            this.initMenu()
        }
    }

    initMenu = (): void => {
        const mm = new MetisMenu('#side-menu')

        let matchingMenuItem = null
        const ul = document.getElementById('side-menu')
        const items = ul.getElementsByTagName('a')
        for (let i = 0; i < items.length; ++i) {
            if (this.props.location.pathname === items[i].pathname) {
                matchingMenuItem = items[i]
                break
            }
        }
        if (matchingMenuItem) {
            this.activateParentDropdown(matchingMenuItem)
        }
    }

    activateParentDropdown = (item: any) => {
        item.classList.add('active')
        const parent = item.parentElement

        if (parent) {
            parent.classList.add('mm-active')
            const parent2 = parent.parentElement

            if (parent2) {
                parent2.classList.add('mm-show')

                const parent3 = parent2.parentElement

                if (parent3) {
                    parent3.classList.add('mm-active') // li
                    parent3.childNodes[0].classList.add('mm-active') // a
                    const parent4 = parent3.parentElement
                    if (parent4) {
                        parent4.classList.add('mm-active')
                    }
                }
            }
            return false
        }
        return false
    }

    render() {
        return (
            <React.Fragment>
                <div className='vertical-menu'>
                    <div data-simplebar className='h-100'>
                        {this.props.type !== 'condensed' ? (
                            // <Scrollbars style={{ maxHeight: '100%' }}>
                            <SidebarContent />
                        ) : (
                            // </Scrollbars>
                            <SidebarContent />
                        )}
                    </div>
                </div>
            </React.Fragment>
        )
    }
}

有人可以告诉我我的代码有什么问题吗

最佳答案

您忘记添加路由器组件。

import { BrowserRouter } from "react-router-dom";

const rootElement = document.getElementById("root");
render(<BrowserRouter><App /></BrowserRouter>, rootElement);

--编辑

如果没有指定Router,则无法使用Link组件。您可以使用BrowserRouter(它使用内部历史API),HashRouter (url hash)或通用Router(您必须为其提供一些配置)

关于reactjs - 不变失败 : You should not use <withRouter(Sidebar)/> outside a <Router>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61855718/

相关文章:

node.js - react 主页属性不起作用

javascript - 如何在 React 中使用 jquery-scrollto?

javascript - 在 react-router 4 中使用 anchor 标签

javascript - 如何在 require.ensure 循环中创建 const ?

javascript - React-Router v4.2.2 不工作(我想我在 Route 标签中做错了什么?)

reactjs - React Bootstrap 自定义导航链接事件样式不起作用

typescript - 如何禁止一个界面的按键出现在另一个界面

typescript - 如何编写一个使用枚举成员的类型而不是枚举本身的通用函数?

javascript - 识别和打印对象中的对象 - Angular

javascript - 如何避免改变 this.state 或 redux 对象