gpt4 book ai didi

javascript - 函数组件不支持contextType

转载 作者:行者123 更新时间:2023-12-03 07:04:24 24 4
gpt4 key购买 nike

Function components do not support contextType.

我在尝试向用 React Router withRouter(...) 函数包装的 React 组件添加上下文时遇到问题。

import React, { Component } from 'react'
import UserContext from './UserContext'
class Toolbar extends Component {
render(){
return (
<div>username: this.context.username</div>
)
}
}
Toolbar.contextType = UserContext
export default withRouter(Toolbar)

最佳答案

这个问题的解决方案就是像这样简单地切换最后两行:

export default withRouter(Toolbar)
Toolbar.contextType = UserContext

关于javascript - 函数组件不支持contextType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54466449/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com