Skip to content

useContext should actuallly return not a, but Maybe a #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
srghma opened this issue Sep 18, 2021 · 3 comments
Closed

useContext should actuallly return not a, but Maybe a #51

srghma opened this issue Sep 18, 2021 · 3 comments

Comments

@srghma
Copy link

srghma commented Sep 18, 2021

it's a usual thing to do in js

export function useData() {
  const context = useContext(DataContext)

  if (!context) {
    throw new Error('Missing Data context')
  }

  return data
}

Maybe introduce major update to the whole react-basic lib, so that all components carry additional phantom type contexts :: ContextList and top React.render should take empty context?

@maddie927
Copy link
Member

maddie927 commented Sep 26, 2021

This is intentional. The createContext function in react-basic requires a default value and that value is used when the current React tree doesn't have a value to use from a parent provider.

createContext :: forall a. a -> Effect (ReactContext a)

If you can't provide a useful default value, you can always make your a a Maybe something.

createContext Nothing :: Effect (ReactContext (Maybe yourType))

If you're running into a runtime error here you're most likely using FFI somewhere, such as specifying in PS the context type of a JS React library. In this case it's really important JS's possible edge cases are caught or you've given the context type the actual JS type (often Nullable something, not Maybe something).

@srghma
Copy link
Author

srghma commented Sep 27, 2021

oh, sorry, I didn't think about that)

@srghma srghma closed this as completed Sep 27, 2021
@maddie927
Copy link
Member

No worries, this should be documented better

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants