Skip to content

Commit fb0262a

Browse files
committed
fix: avoid rendering Context directly warning
1 parent b1cf575 commit fb0262a

File tree

5 files changed

+1199
-67
lines changed

5 files changed

+1199
-67
lines changed

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@
5959
"@4c/semantic-release-config": "^1.0.2",
6060
"@babel/cli": "^7.0.0-beta.39",
6161
"@babel/core": "^7.0.0-beta.39",
62-
"@monastic.panic/enzyme-adapter-react-16": "^1.2.2",
6362
"babel-core": "bridge",
6463
"babel-eslint": "^8.2.1",
6564
"babel-jest": "^22.4.3",
6665
"codecov": "^3.0.2",
67-
"enzyme": "^3.3.0",
66+
"enzyme": "^3.7.0",
67+
"enzyme-adapter-react-16": "^1.6.0",
6868
"eslint": "^4.16.0",
6969
"eslint-config-4catalyzer-react": "^0.4.1",
7070
"eslint-config-prettier": "^2.9.0",
@@ -77,8 +77,8 @@
7777
"jest": "^22.4.3",
7878
"lint-staged": "^7.1.0",
7979
"prettier": "^1.10.2",
80-
"react": "^16.3.2",
81-
"react-dom": "^16.3.2",
80+
"react": "^16.6.0",
81+
"react-dom": "^16.6.0",
8282
"semantic-release": "^15.4.0",
8383
"travis-deploy-once": "^5.0.0"
8484
},

src/mapContextToProps.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const getDisplayName = Component => {
99
return name ? `ContextTransform(${name})` : 'ContextTransform';
1010
};
1111

12+
const ensureConsumer = c => c.Consumer || c;
13+
1214
function $mapContextToProps(
1315
{
1416
consumers: maybeArrayOfConsumers,
@@ -23,7 +25,7 @@ function $mapContextToProps(
2325
consumers = [maybeArrayOfConsumers];
2426
}
2527

26-
const SingleConsumer = consumers[0];
28+
const SingleConsumer = ensureConsumer(consumers[0]);
2729
function singleRender(props, ref) {
2830
const propsWithRef = { [forwardRefAs]: ref, ...props };
2931
return (
@@ -38,9 +40,10 @@ function $mapContextToProps(
3840
function multiRender(props, ref) {
3941
const propsWithRef = { [forwardRefAs]: ref, ...props };
4042
return consumers.reduceRight(
41-
(inner, Consumer) => (...args) => (
42-
<Consumer>{value => inner(...args, value)}</Consumer>
43-
),
43+
(inner, Context) => (...args) => {
44+
const Consumer = ensureConsumer(Context);
45+
return <Consumer>{value => inner(...args, value)}</Consumer>;
46+
},
4447
(...contexts) => (
4548
<Component {...propsWithRef} {...mapToProps(...contexts, props)} />
4649
),

test/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import { configure } from 'enzyme';
2-
import Adapter from '@monastic.panic/enzyme-adapter-react-16';
2+
import Adapter from 'enzyme-adapter-react-16';
33

44
configure({ adapter: new Adapter() });

test/mapContextToProps.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,16 @@ describe('mapContextToProps', () => {
137137

138138
expect(Mapper.render.displayName).toEqual('WithIntl');
139139
});
140+
141+
it('should not warn about Contexts', () => {
142+
const Mapper = mapContextToProps(
143+
{
144+
consumers: React.createContext('foo'),
145+
mapToProps: foo => ({ children: foo }),
146+
displayName: 'WithIntl',
147+
},
148+
'div',
149+
);
150+
mount(<Mapper />);
151+
});
140152
});

0 commit comments

Comments
 (0)