-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Played around with context api, tried to get the wrapped component to rerender after global context was updated - this is what I came up with. Note: this commit makes the playground app use a newer js core as the js core which shipped with RN on Android is missing javascript Proxy. Closes #4517
- Loading branch information
Showing
6 changed files
with
55 additions
and
15 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,32 @@ | ||
const React = require('react'); | ||
const titleContext = React.createContext('Default title from Context'); | ||
let ctx = { | ||
title: 'Title from global context', | ||
count: 0 | ||
}; | ||
|
||
const stateAwareContext = (component) => | ||
new Proxy(ctx, { | ||
set: function (obj, prop, value) { | ||
obj[prop] = value; | ||
component.setState({ context: stateAwareContext(component) }); | ||
return true; | ||
} | ||
}); | ||
|
||
const GlobalContext = React.createContext({}); | ||
class ContextProvider extends React.Component { | ||
state = {context :stateAwareContext(this)}; | ||
render() { | ||
return ( | ||
<GlobalContext.Provider value={this.state.context}> | ||
{this.props.children} | ||
</GlobalContext.Provider> | ||
); | ||
} | ||
} | ||
|
||
module.exports = { | ||
TitleContext: titleContext, | ||
ContextProvider, | ||
GlobalContext, | ||
Context: React.createContext('Default value from Context') | ||
} |
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
dd41d48
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.
@guyca Do you know that JSC on iOS 9 also do not support Proxy?
dd41d48
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.
No, I didn't know that.