Home > Software design >  i have imported everything but still it throws this error TypeError: can't access property &quo
i have imported everything but still it throws this error TypeError: can't access property &quo

Time:12-20

when i imported postDetails in another component it says postDetails is undefined

import {createContext, useState} from 'react'


export const PostContext = createContext(null)

function Post({children}){

    const [postDetails, setPostDetails] = useState('')


    return(
        <PostContext.Provider value={ postDetails , setPostDetails } >
          {children}
        </PostContext.Provider>
    )

}

export default Post

CodePudding user response:

Value accepts an object (particularly one thing, same as all other props), wrap your value in an object. see value on PostContext.Provider line:

 return(
        <PostContext.Provider value={{ postDetails , setPostDetails }} >
          {children}
        </PostContext.Provider>
    )
  • Related