admin管理员组文章数量:1023204
Function ponents do not support contextType.
I encountered an issue when trying to add context to a React ponent that was wrapped with a React Router withRouter(...)
function.
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)
Function ponents do not support contextType.
I encountered an issue when trying to add context to a React ponent that was wrapped with a React Router withRouter(...)
function.
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)
Share
Improve this question
edited Jan 31, 2019 at 21:32
Tom Hofman
asked Jan 31, 2019 at 17:48
Tom HofmanTom Hofman
5205 silver badges21 bronze badges
1 Answer
Reset to default 6The solution to this issue is simply switch the last two lines like so:
export default withRouter(Toolbar)
Toolbar.contextType = UserContext
Function ponents do not support contextType.
I encountered an issue when trying to add context to a React ponent that was wrapped with a React Router withRouter(...)
function.
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)
Function ponents do not support contextType.
I encountered an issue when trying to add context to a React ponent that was wrapped with a React Router withRouter(...)
function.
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)
Share
Improve this question
edited Jan 31, 2019 at 21:32
Tom Hofman
asked Jan 31, 2019 at 17:48
Tom HofmanTom Hofman
5205 silver badges21 bronze badges
1 Answer
Reset to default 6The solution to this issue is simply switch the last two lines like so:
export default withRouter(Toolbar)
Toolbar.contextType = UserContext
本文标签: javascriptFunction components do not support contextTypeStack Overflow
版权声明:本文标题:javascript - Function components do not support contextType - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745506970a2153648.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论