Skip to content
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

Upgrade to Preact X #2

Merged
merged 3 commits into from Sep 4, 2019
Merged

Conversation

swashcap
Copy link
Collaborator

@swashcap swashcap commented Aug 21, 2019

  • Problem: Preact X changed the context API, which this package uses to pass down store. A connect-ed component doesn't appear to render at all!
  • Solution: Use the new createContext export and its consumer and provider.
  • Testing: Just run npm test!

Note: This PR should probably correspond to changing peerDependencies.preact to >=10.x.x in the package.json. I should be published to npm with a next tag or major version bump.

export const Provider = (props) => {
const store = props.store
const rest = {}
Object.keys(props).forEach((key) => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

microbundle doesn't like the spread operator. Just do it manually here.

}
})

return h(StoreContext.Provider, Object.assign({ value: store }, rest))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Map props.store to the context provider's expected prop, value.

@@ -48,4 +56,8 @@ export const connect = (...args) => {
return h(Comp, Object.assign({}, props, state, this.actionCreators))
}
}

return (props) => h(StoreContext.Consumer, undefined, (store) => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only way to consume context in the new API is through the context Consumer or the useContext hook. This seems like the simplest solution.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this, very clean.

@swashcap swashcap merged commit 852dfa4 into HenrikJoreteg:master Sep 4, 2019
@swashcap swashcap deleted the enhancement/preact-x branch September 4, 2019 23:09
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

Successfully merging this pull request may close these issues.

None yet

2 participants