react-context
Version:
Helpful Properties with React Context
47 lines (40 loc) • 1.32 kB
Markdown
id: usage-set-types
title: Set Types
Any child below your root component can now subscribe to any of the context types by defining `contextTypes`.
Use the `context.subscribe` function as a shortcut to avoid having to define context types.
```
var React = require('react');
var context = require('react-context');
var Child = React.createClass({
render(){
return <div>{ this.context.focus }</div>
}
});
Child.contextTypes = context.subscribe(['focus']);
module.exports = Child;
```
Pass it an array of types you want to subscribe to, or call it with no arguments to subscribe to all of them.
```
// Subscribe to scroll and adBlock
Child.contextTypes = context.subscribe(['scroll', 'adBlock']);
// Subscribe to every context
Child.contextTypes = context.subscribe();
```
Otherwise, you can use `contextTypes` the traditional way and include the types you want to subscribe to:
```javascript
Child.contextTypes = {
pointer: React.PropTypes.string,
density: React.PropTypes.number,
width: React.PropTypes.number,
height: React.PropTypes.number,
language: React.PropTypes.string,
focus: React.PropTypes.bool,
scroll: React.PropTypes.number,
adBlock: React.PropTypes.bool,
os: React.PropTypes.string,
browser: React.PropTypes.string,
browserVersion: React.PropTypes.string
};
```