Last active
April 19, 2018 14:32
-
-
Save 505aaron/01e3fdab5fbf59ece70181c4a0838739 to your computer and use it in GitHub Desktop.
Medium Gist
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
const { Provider, Consumer } = React.createContext(buildInitialContext()); | |
export class DimensionProvider extends Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
data: buildInitialContext() | |
}; | |
} | |
componentDidMount() { | |
Dimensions.addEventListener("change", this.dimensionHandler); | |
} | |
componentWillUnmount() { | |
Dimensions.removeEventListener("change", this.dimensionHandler); | |
} | |
dimensionHandler = ({ window, screen }) => { | |
this.setState(({ data }) => ({ | |
data: updateState(data, window, screen) | |
})); | |
}; | |
render() { | |
return <Provider value={this.state.data}>{this.props.children}</Provider>; | |
} | |
} | |
export const WindowConsumer = ({ children }) => { | |
return ( | |
<Consumer> | |
{dimensions => | |
children(getWindowWidth(dimensions), getWindowHeight(dimensions)) | |
} | |
</Consumer> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment