Polisas
Polisas

Reputation: 541

getContext and withContext not working

I've been trying to pass id to other component with context, but I get undefined, somewhere i'm making an error. As I understand we should get context as props. Any Ideas ?

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)

Upvotes: 2

Views: 2977

Answers (1)

Martin Dawson
Martin Dawson

Reputation: 7656

Context only works from parents to children by passing props down, not siblings.

Make ComponentTwo a child of ComponentOne.

Upvotes: 2

Related Questions