我一直在尝试将 id 传递给带有上下文的其他组件,但是我得到了未定义的东西,我在某个地方出错了。据我了解,我们应该将上下文作为道具。有任何想法吗 ?
import {compose,withContext} from 'recompose'
const ComponentOne = ({id}) => {
console.log(id) // cizlory7iji600149711su9vj
...
}
const Context = withContext(
{id:React.PropTypes.string},
(props) => ({id:props.id})
)
export default compose(Context)(ComponentOne)
SecondComponent.js
import {compose,getContext} from 'recompose'
const ComponentTwo = ({id}) => {
console.log(id) // undefined
...
}
const GetContext = getContext(
{id:React.PropTypes.string}
)
export default compose(GetContext)(ComponentTwo)