├── .eslintignore ├── example └── src │ ├── .gitignore │ ├── example.js │ ├── index.html │ └── example.less ├── .travis.yml ├── .editorconfig ├── src ├── Three.js ├── ContextExample.js ├── One.js └── Two.js ├── .gitignore ├── .eslintrc ├── gulpfile.js ├── LICENSE ├── test ├── ContextExample.js ├── Three.js ├── Two.js └── One.js ├── package.json ├── dist ├── react-context-example.min.js └── react-context-example.js ├── README.md └── lib └── ContextExample.js /.eslintignore: -------------------------------------------------------------------------------- 1 | .publish/* 2 | dist/* 3 | example/dist/* 4 | lib/* 5 | node_modules/* 6 | -------------------------------------------------------------------------------- /example/src/.gitignore: -------------------------------------------------------------------------------- 1 | ## This file is here to ensure it is included in the gh-pages branch, 2 | ## when `gulp deploy` is used to push updates to the demo site. 3 | 4 | # Dependency directory 5 | node_modules 6 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | before_install: 3 | - "npm install npm -g" 4 | node_js: 5 | - "0.10" 6 | - "0.12" 7 | - "iojs" 8 | env: 9 | - TEST_SUITE=lint 10 | - TEST_SUITE=unit 11 | script: "npm run-script $TEST_SUITE" 12 | -------------------------------------------------------------------------------- /example/src/example.js: -------------------------------------------------------------------------------- 1 | var React = require('react'); 2 | var ContextExample = require('react-context-example'); 3 | 4 | var App = React.createClass({ 5 | render () { 6 | return ( 7 |
8 | 9 |
10 | ); 11 | } 12 | }); 13 | 14 | React.render(, document.getElementById('app')); 15 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # This file is for unifying the coding style for different editors and IDEs 2 | # editorconfig.org 3 | root = true 4 | 5 | [*] 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = false 9 | insert_final_newline = true 10 | indent_style = tab 11 | 12 | [*.json] 13 | indent_style = space 14 | indent_size = 2 15 | -------------------------------------------------------------------------------- /src/Three.js: -------------------------------------------------------------------------------- 1 | var React = require('react'); 2 | 3 | var Three = React.createClass({ 4 | contextTypes: { 5 | a: React.PropTypes.string, 6 | b: React.PropTypes.string 7 | }, 8 | render() { 9 | return ( 10 |
11 | Three 12 | ({this.context.a}, {this.context.b}) 13 |
14 | ); 15 | } 16 | }); 17 | 18 | export default Three; 19 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Coverage tools 11 | lib-cov 12 | coverage 13 | coverage.html 14 | .cover* 15 | 16 | # Dependency directory 17 | node_modules 18 | 19 | # Example build directory 20 | .publish 21 | example/dist 22 | 23 | # Editor and other tmp files 24 | *.swp 25 | *.un~ 26 | *.iml 27 | *.ipr 28 | *.iws 29 | *.sublime-* 30 | .idea/ 31 | *.DS_Store 32 | -------------------------------------------------------------------------------- /src/ContextExample.js: -------------------------------------------------------------------------------- 1 | var React = require('react'); 2 | var One = require('./One'); 3 | 4 | var ContextExample = React.createClass({ 5 | propTypes: { 6 | a: React.PropTypes.string 7 | }, 8 | childContextTypes: { 9 | a: React.PropTypes.string 10 | }, 11 | getChildContext() { 12 | return { 13 | a: this.props.a 14 | } 15 | }, 16 | render() { 17 | return ( 18 |
19 | Context 20 | 21 |
22 | ); 23 | } 24 | 25 | }); 26 | 27 | export default ContextExample; 28 | -------------------------------------------------------------------------------- /src/One.js: -------------------------------------------------------------------------------- 1 | var React = require('react'); 2 | var Two = require('./Two'); 3 | 4 | var One = React.createClass({ 5 | propTypes: { 6 | b: React.PropTypes.string 7 | }, 8 | contextTypes: { 9 | a: React.PropTypes.string 10 | }, 11 | childContextTypes: { 12 | b: React.PropTypes.string 13 | }, 14 | getChildContext() { 15 | return { 16 | b: this.props.b 17 | } 18 | }, 19 | render() { 20 | return ( 21 |
22 | One 23 | ({this.context.a}) 24 | 25 |
26 | ); 27 | } 28 | }); 29 | 30 | export default One; 31 | -------------------------------------------------------------------------------- /src/Two.js: -------------------------------------------------------------------------------- 1 | var React = require('react'); 2 | var Three = require('./Three'); 3 | 4 | var Two = React.createClass({ 5 | contextTypes: { 6 | a: React.PropTypes.string, 7 | b: React.PropTypes.string 8 | }, 9 | childContextTypes: { 10 | a: React.PropTypes.string 11 | }, 12 | getChildContext() { 13 | return { 14 | a: this.props.a || this.context.a 15 | } 16 | }, 17 | render() { 18 | return ( 19 |
20 | Two 21 | ({this.context.a}, {this.context.b}) 22 | 23 |
24 | ); 25 | } 26 | }); 27 | 28 | export default Two; 29 | -------------------------------------------------------------------------------- /example/src/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Context Example 4 | 5 | 6 | 7 |
8 |

Context Example

9 |

View project on GitHub

10 | 11 |
12 |
13 | 14 |
15 | 18 |
19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "parser": "babel-eslint", 3 | "env": { 4 | "browser": true, 5 | "node": true 6 | }, 7 | "plugins": [ 8 | "react" 9 | ], 10 | "rules": { 11 | "curly": [2, "multi-line"], 12 | "quotes": [2, "single", "avoid-escape"], 13 | "react/display-name": 0, 14 | "react/jsx-boolean-value": 1, 15 | "react/jsx-quotes": 1, 16 | "react/jsx-no-undef": 1, 17 | "react/jsx-sort-props": 0, 18 | "react/jsx-sort-prop-types": 1, 19 | "react/jsx-uses-react": 1, 20 | "react/jsx-uses-vars": 1, 21 | "react/no-did-mount-set-state": 1, 22 | "react/no-did-update-set-state": 1, 23 | "react/no-multi-comp": 1, 24 | "react/no-unknown-property": 1, 25 | "react/prop-types": 1, 26 | "react/react-in-jsx-scope": 1, 27 | "react/self-closing-comp": 1, 28 | "react/wrap-multilines": 1, 29 | "semi": 2, 30 | "strict": 0 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /gulpfile.js: -------------------------------------------------------------------------------- 1 | var gulp = require('gulp'); 2 | var initGulpTasks = require('react-component-gulp-tasks'); 3 | 4 | /** 5 | * Tasks are added by the react-component-gulp-tasks package 6 | * 7 | * See https://github.com/JedWatson/react-component-gulp-tasks 8 | * for documentation. 9 | * 10 | * You can also add your own additional gulp tasks if you like. 11 | */ 12 | 13 | var taskConfig = { 14 | 15 | component: { 16 | name: 'ContextExample', 17 | dependencies: [ 18 | 'classnames', 19 | 'react', 20 | 'react/addons' 21 | ], 22 | lib: 'lib' 23 | }, 24 | 25 | example: { 26 | src: 'example/src', 27 | dist: 'example/dist', 28 | files: [ 29 | 'index.html', 30 | '.gitignore' 31 | ], 32 | scripts: [ 33 | 'example.js' 34 | ], 35 | less: [ 36 | 'example.less' 37 | ] 38 | } 39 | 40 | }; 41 | 42 | initGulpTasks(gulp, taskConfig); 43 | -------------------------------------------------------------------------------- /example/src/example.less: -------------------------------------------------------------------------------- 1 | /* 2 | // Examples Stylesheet 3 | // ------------------- 4 | */ 5 | 6 | body { 7 | font-family: Helvetica Neue, Helvetica, Arial, sans-serif; 8 | font-size: 14px; 9 | color: #333; 10 | margin: 0; 11 | padding: 0; 12 | } 13 | 14 | a { 15 | color: #08c; 16 | text-decoration: none; 17 | } 18 | 19 | a:hover { 20 | text-decoration: underline; 21 | } 22 | 23 | .container { 24 | margin-left: auto; 25 | margin-right: auto; 26 | max-width: 720px; 27 | padding: 1em; 28 | } 29 | 30 | .footer { 31 | margin-top: 50px; 32 | border-top: 1px solid #eee; 33 | padding: 20px 0; 34 | font-size: 12px; 35 | color: #999; 36 | } 37 | 38 | h1, h2, h3, h4, h5, h6 { 39 | color: #222; 40 | font-weight: 100; 41 | margin: 0.5em 0; 42 | } 43 | 44 | label { 45 | color: #999; 46 | display: inline-block; 47 | font-size: 0.85em; 48 | font-weight: bold; 49 | margin: 1em 0; 50 | text-transform: uppercase; 51 | } 52 | 53 | .hint { 54 | margin: 15px 0; 55 | font-style: italic; 56 | color: #999; 57 | } 58 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Jed Watson 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /test/ContextExample.js: -------------------------------------------------------------------------------- 1 | /* global describe, it, beforeEach */ 2 | 3 | var assert = require('assert'); 4 | var jsdom = require('mocha-jsdom'); 5 | 6 | var React = require('react/addons'); 7 | var TestUtils = React.addons.TestUtils; 8 | 9 | var ContextExample = require('../src/ContextExample'); 10 | 11 | describe('ContextExample', function () { 12 | jsdom(); 13 | 14 | var render; 15 | 16 | beforeEach(function () { 17 | render = TestUtils.renderIntoDocument(React.createElement(ContextExample, { a: 'Aye' })); 18 | }); 19 | 20 | it('renders three levels of context', function () { 21 | var levels = TestUtils.scryRenderedDOMComponentsWithTag(render, 'div'); 22 | assert.equal(levels.length, 4); 23 | }); 24 | 25 | it('renders ContextExample prop via context at one level deep', function () { 26 | var content = render.getDOMNode().textContent; 27 | assert.notEqual(content.indexOf('One (Aye)'), -1); 28 | }); 29 | 30 | it('renders ContentExample prop via context and One prop via context at two levels deep', function () { 31 | var content = render.getDOMNode().textContent; 32 | assert.notEqual(content.indexOf('Two (Aye, Bee)'), -1); 33 | }); 34 | 35 | it('renders Two prop via context and One prop via context at three levels deep', function () { 36 | var content = render.getDOMNode().textContent; 37 | assert.notEqual(content.indexOf('Three (Zed, Bee)'), -1); 38 | }); 39 | }); 40 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-context-example", 3 | "version": "1.0.0", 4 | "description": "Context Example", 5 | "main": "lib/ContextExample.js", 6 | "author": "Jed Watson", 7 | "homepage": "https://github.com/JedWatson/react-context-example", 8 | "repository": { 9 | "type": "git", 10 | "url": "https://github.com/JedWatson/react-context-example.git" 11 | }, 12 | "bugs": { 13 | "url": "https://github.com/JedWatson/react-context-example/issues" 14 | }, 15 | "dependencies": { 16 | "classnames": "^2.1.1", 17 | "react": "^0.13.3" 18 | }, 19 | "devDependencies": { 20 | "babel": "^5.4.7", 21 | "gulp": "^3.9.0", 22 | "happiness": "^1.0.3", 23 | "jsdom": "^3.1.2", 24 | "mocha": "^2.2.5", 25 | "mocha-jsdom": "^0.4.0", 26 | "react-component-gulp-tasks": "^0.7.0", 27 | "react-stub-context": "^0.3.0" 28 | }, 29 | "browserify-shim": { 30 | "react": "global:React" 31 | }, 32 | "scripts": { 33 | "build": "gulp clean && NODE_ENV=production gulp build", 34 | "examples": "gulp dev:server", 35 | "lint": "happiness", 36 | "publish:site": "gulp publish:examples", 37 | "release": "gulp release", 38 | "start": "gulp dev", 39 | "unit": "mocha --compilers js:babel/register", 40 | "test": "npm run lint && npm run unit", 41 | "watch": "gulp watch:lib" 42 | }, 43 | "keywords": [ 44 | "react", 45 | "context", 46 | "example" 47 | ] 48 | } 49 | -------------------------------------------------------------------------------- /test/Three.js: -------------------------------------------------------------------------------- 1 | /* global describe, it */ 2 | 3 | var assert = require('assert'); 4 | var jsdom = require('mocha-jsdom'); 5 | 6 | var React = require('react/addons'); 7 | var TestUtils = React.addons.TestUtils; 8 | var stubContext = require('react-stub-context'); 9 | 10 | var Three = require('../src/Three'); 11 | 12 | describe('Three', function () { 13 | jsdom(); 14 | 15 | var levels, content, render; 16 | 17 | it('renders one level deep', function () { 18 | render = TestUtils.renderIntoDocument( 19 | React.createElement(Three) 20 | ); 21 | 22 | levels = TestUtils.scryRenderedDOMComponentsWithTag(render, 'div'); 23 | assert.equal(levels.length, 1); 24 | }); 25 | 26 | describe('without context', function () { 27 | it('renders nothing without context from parents at one level deep', function () { 28 | render = TestUtils.renderIntoDocument( 29 | React.createElement(Three) 30 | ); 31 | 32 | content = render.getDOMNode().textContent; 33 | assert.notEqual(content.indexOf('Three (, )'), -1); 34 | }); 35 | }); 36 | 37 | describe('with context', function () { 38 | it('renders context from parents at one level deep', function () { 39 | Three = stubContext(Three, { a: 'Aye', b: 'Bee' }); 40 | 41 | render = TestUtils.renderIntoDocument( 42 | React.createElement(Three) 43 | ); 44 | 45 | content = render.getDOMNode().textContent; 46 | assert.notEqual(content.indexOf('Three (Aye, Bee)'), -1); 47 | }); 48 | }); 49 | }); 50 | -------------------------------------------------------------------------------- /dist/react-context-example.min.js: -------------------------------------------------------------------------------- 1 | !function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var t;t="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,t.ContextExample=e()}}(function(){return function e(t,n,r){function o(s,p){if(!n[s]){if(!t[s]){var l="function"==typeof require&&require;if(!p&&l)return l(s,!0);if(i)return i(s,!0);var a=new Error("Cannot find module '"+s+"'");throw a.code="MODULE_NOT_FOUND",a}var f=n[s]={exports:{}};t[s][0].call(f.exports,function(e){var n=t[s][1][e];return o(n?n:e)},f,f.exports,e,t,n,r)}return n[s].exports}for(var i="function"==typeof require&&require,s=0;s