-
-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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
Make <Link> work with static containers (take two) #3430
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import React, { PropTypes } from 'react' | ||
|
||
// Works around issues with context updates failing to propagate. | ||
// https://github.com/facebook/react/issues/2517 | ||
// https://github.com/reactjs/react-router/issues/470 | ||
|
||
export function createContextProvider(name, contextType = PropTypes.any) { | ||
const ContextProvider = React.createClass({ | ||
propTypes: { | ||
children: PropTypes.node.isRequired | ||
}, | ||
|
||
contextTypes: { | ||
[name]: contextType | ||
}, | ||
|
||
childContextTypes: { | ||
[name]: contextType | ||
}, | ||
|
||
getChildContext() { | ||
return { | ||
[name]: { | ||
...this.context[name], | ||
subscribe: this.subscribe, | ||
eventIndex: this.eventIndex | ||
} | ||
} | ||
}, | ||
|
||
componentWillMount() { | ||
this.eventIndex = 0 | ||
this.listeners = [] | ||
}, | ||
|
||
componentWillReceiveProps() { | ||
this.eventIndex++ | ||
}, | ||
|
||
componentDidUpdate() { | ||
this.listeners.forEach(listener => listener(this.eventIndex)) | ||
}, | ||
|
||
subscribe(listener) { | ||
// No need to immediately call listener here. | ||
this.listeners.push(listener) | ||
|
||
return () => { | ||
this.listeners = this.listeners.filter(item => item !== listener) | ||
} | ||
}, | ||
|
||
render() { | ||
return this.props.children | ||
} | ||
}) | ||
|
||
return ContextProvider | ||
} | ||
|
||
export function connectToContext(WrappedComponent, name, contextType = PropTypes.any) { | ||
const ContextSubscriber = React.createClass({ | ||
contextTypes: { | ||
[name]: contextType | ||
}, | ||
|
||
getInitialState() { | ||
if (!this.context[name]) { | ||
return {} | ||
} | ||
|
||
return { | ||
lastRenderedEventIndex: this.context[name].eventIndex | ||
} | ||
}, | ||
|
||
componentDidMount() { | ||
if (!this.context[name]) { | ||
return | ||
} | ||
|
||
this.unsubscribe = this.context[name].listen(eventIndex => { | ||
if (eventIndex !== this.state.lastRenderedEventIndex) { | ||
this.setState({ lastRenderedEventIndex: eventIndex }) | ||
} | ||
}) | ||
}, | ||
|
||
componentWillReceiveProps() { | ||
if (!this.context[name]) { | ||
return | ||
} | ||
|
||
this.setState({ | ||
lastRenderedEventIndex: this.context[name].eventIndex | ||
}) | ||
}, | ||
|
||
componentWillUnmount() { | ||
if (!this.unsubscribe) { | ||
return | ||
} | ||
|
||
this.unsubscribe() | ||
this.unsubscribe = null | ||
}, | ||
|
||
render() { | ||
return <WrappedComponent {...this.props} /> | ||
} | ||
}) | ||
|
||
return ContextSubscriber | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the name of this isn't clear to me that's it's going to subscribe to some thing with any random name that happens to have a
listen
method. Why all the abstraction? Why not just getrouter
off of context directly and listen to changes?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to eventually pull this out into a separate library. There are a number of other React libraries that use context in this sort of pattern, and they all have the same issue of not playing well with Redux and Relay containers that implement SCU optimizations, and I'd like to use this functionality there as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with @ryanflorence here. This doesn't feel like the right place for all of this. I know you want to build out a separate library, but I don't think it's the right place to live-test this approach. We already have a
listen
androuter
is on context, so this can probably be simplified quite a bit using existing code.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are some bugs here that I will fix. Would this look better if it actually were a separate library?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why no decorator syntax? 😢
To allow:
I know it's still experimental and all, but...it's so concise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We already have that with
withRouter
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't think of it. 😛
On the other hand, the subscriber is really only meant to be used in a very few places – not in application code unless working around a library that doesn't do this, so I don't think we need to make as many affordances for DX as on e.g. Redux
@connect
or something.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a public API anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not of this library, anyway 😄