Skip to content

Commit d99a17c

Browse files
committed
Less magic is better for the future generations.
1 parent 65e6340 commit d99a17c

19 files changed

+269
-153
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"presets": ["latest"]
2+
"presets": ["latest"],
3+
"plugins": ["transform-class-properties"]
34
}

packages/react-scripts/fixtures/kitchensink/.template.dependencies.json

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"babel-preset-latest": "6.16.0",
44
"babel-register": "6.22.0",
55
"babel-polyfill": "6.20.0",
6+
"babel-plugin-transform-class-properties": "6.22.0",
67
"chai": "3.5.0",
78
"jsdom": "9.8.3",
89
"mocha": "3.2.0"

packages/react-scripts/fixtures/kitchensink/src/App.js

+27-15
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,33 @@
1-
import React from 'react';
1+
import React, { Component, PropTypes, createElement } from 'react';
22

33
class BuiltEmitter extends React.Component {
4-
constructor(props) {
5-
super(props)
6-
7-
this.callWhenDone = done => done();
4+
static propTypes = {
5+
feature: PropTypes.func.isRequired
86
}
97

108
componentDidMount() {
11-
this.callWhenDone(() => document.dispatchEvent(new Event('ReactFeatureDidMount')));
9+
const { feature } = this.props
10+
if (!Component.isPrototypeOf(feature)) {
11+
this.notifyRendered();
12+
}
1213
}
1314

14-
render() {
15-
const feature = React.cloneElement(React.Children.only(this.props.children), {
16-
setCallWhenDone: done => {
17-
this.callWhenDone = done;
18-
}
19-
});
15+
notifyRendered() {
16+
document.dispatchEvent(new Event('ReactFeatureDidMount'));
17+
}
2018

21-
return <div>{feature}</div>;
19+
render() {
20+
const {
21+
props: { feature },
22+
notifyRendered
23+
} = this;
24+
return (
25+
<div>
26+
{createElement(feature, {
27+
notifyRendered
28+
})}
29+
</div>
30+
);
2231
}
2332
}
2433

@@ -116,8 +125,11 @@ class App extends React.Component {
116125
}
117126

118127
render() {
119-
const Feature = this.state.feature;
120-
return Feature ? <BuiltEmitter><Feature /></BuiltEmitter> : null;
128+
const { feature } = this.state;
129+
if (feature !== null) {
130+
return <BuiltEmitter feature={feature} />;
131+
}
132+
return null
121133
}
122134
}
123135

packages/react-scripts/fixtures/kitchensink/src/features/env/NodePath.js

+15-9
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
1-
import React from 'react'
1+
import React, { Component, PropTypes } from 'react'
22
import load from 'absoluteLoad'
33

4-
export default class extends React.Component {
5-
constructor(props) {
6-
super(props);
4+
export default class extends Component {
5+
static propTypes = {
6+
notifyRendered: PropTypes.func
7+
}
78

8-
this.done = () => {};
9-
this.props.setCallWhenDone && this.props.setCallWhenDone((done) => {
10-
this.done = done;
11-
});
9+
static defaultProps = {
10+
notifyRendered: () => {}
11+
}
1212

13+
constructor(props) {
14+
super(props);
1315
this.state = { users: [] };
1416
}
1517

1618
async componentDidMount() {
1719
const users = load();
18-
this.setState({ users }, () => this.done());
20+
this.setState({ users });
21+
}
22+
23+
componentDidUpdate() {
24+
this.props.notifyRendered();
1925
}
2026

2127
render() {

packages/react-scripts/fixtures/kitchensink/src/features/syntax/ArrayDestructuring.js

+15-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react'
1+
import React, { Component, PropTypes } from 'react'
22

33
function load() {
44
return [
@@ -9,21 +9,27 @@ function load() {
99
];
1010
}
1111

12-
export default class extends React.Component {
13-
constructor(props) {
14-
super(props);
12+
export default class extends Component {
13+
static propTypes = {
14+
notifyRendered: PropTypes.func
15+
}
1516

16-
this.done = () => {};
17-
this.props.setCallWhenDone && this.props.setCallWhenDone((done) => {
18-
this.done = done;
19-
});
17+
static defaultProps = {
18+
notifyRendered: () => {}
19+
}
2020

21+
constructor(props) {
22+
super(props);
2123
this.state = { users: [] };
2224
}
2325

2426
async componentDidMount() {
2527
const users = load();
26-
this.setState({ users }, () => this.done());
28+
this.setState({ users });
29+
}
30+
31+
componentDidUpdate() {
32+
this.props.notifyRendered();
2733
}
2834

2935
render() {

packages/react-scripts/fixtures/kitchensink/src/features/syntax/ArraySpread.js

+15-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react'
1+
import React, { Component, PropTypes } from 'react'
22

33
function load(users) {
44
return [
@@ -9,21 +9,27 @@ function load(users) {
99
];
1010
}
1111

12-
export default class extends React.Component {
13-
constructor(props) {
14-
super(props);
12+
export default class extends Component {
13+
static propTypes = {
14+
notifyRendered: PropTypes.func
15+
}
1516

16-
this.done = () => {};
17-
this.props.setCallWhenDone && this.props.setCallWhenDone((done) => {
18-
this.done = done;
19-
});
17+
static defaultProps = {
18+
notifyRendered: () => {}
19+
}
2020

21+
constructor(props) {
22+
super(props);
2123
this.state = { users: [] };
2224
}
2325

2426
async componentDidMount() {
2527
const users = load([{ id: 42, name: '42' }]);
26-
this.setState({ users }, () => this.done());
28+
this.setState({ users });
29+
}
30+
31+
componentDidUpdate() {
32+
this.props.notifyRendered();
2733
}
2834

2935
render() {

packages/react-scripts/fixtures/kitchensink/src/features/syntax/AsyncAwait.js

+15-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react'
1+
import React, { Component, PropTypes } from 'react'
22

33
async function load() {
44
return [
@@ -9,21 +9,27 @@ async function load() {
99
];
1010
}
1111

12-
export default class extends React.Component {
13-
constructor(props) {
14-
super(props);
12+
export default class extends Component {
13+
static propTypes = {
14+
notifyRendered: PropTypes.func
15+
}
1516

16-
this.done = () => {};
17-
this.props.setCallWhenDone && this.props.setCallWhenDone((done) => {
18-
this.done = done;
19-
});
17+
static defaultProps = {
18+
notifyRendered: () => {}
19+
}
2020

21+
constructor(props) {
22+
super(props);
2123
this.state = { users: [] };
2224
}
2325

2426
async componentDidMount() {
2527
const users = await load();
26-
this.setState({ users }, () => this.done());
28+
this.setState({ users });
29+
}
30+
31+
componentDidUpdate() {
32+
this.props.notifyRendered();
2733
}
2834

2935
render() {

packages/react-scripts/fixtures/kitchensink/src/features/syntax/ClassProperties.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
1-
import React from 'react'
1+
import React, { Component, PropTypes } from 'react'
2+
3+
export default class extends Component {
4+
static propTypes = {
5+
notifyRendered: PropTypes.func
6+
}
7+
8+
static defaultProps = {
9+
notifyRendered: () => {}
10+
}
211

3-
export default class extends React.Component {
412
users = [
513
{ id: 1, name: '1' },
614
{ id: 2, name: '2' },
715
{ id: 3, name: '3' },
816
{ id: 4, name: '4' }
917
];
1018

19+
componentDidMount() {
20+
this.props.notifyRendered()
21+
}
22+
1123
render() {
1224
return (
1325
<div id="feature-class-properties">

packages/react-scripts/fixtures/kitchensink/src/features/syntax/ComputedProperties.js

+15-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react'
1+
import React, { Component, PropTypes } from 'react'
22

33
function load(prefix) {
44
return [
@@ -9,21 +9,27 @@ function load(prefix) {
99
];
1010
}
1111

12-
export default class extends React.Component {
13-
constructor(props) {
14-
super(props);
12+
export default class extends Component {
13+
static propTypes = {
14+
notifyRendered: PropTypes.func
15+
}
1516

16-
this.done = () => {};
17-
this.props.setCallWhenDone && this.props.setCallWhenDone((done) => {
18-
this.done = done;
19-
});
17+
static defaultProps = {
18+
notifyRendered: () => {}
19+
}
2020

21+
constructor(props) {
22+
super(props);
2123
this.state = { users: [] };
2224
}
2325

2426
async componentDidMount() {
2527
const users = load('user_');
26-
this.setState({ users }, () => this.done());
28+
this.setState({ users });
29+
}
30+
31+
componentDidUpdate() {
32+
this.props.notifyRendered();
2733
}
2834

2935
render() {

packages/react-scripts/fixtures/kitchensink/src/features/syntax/CustomInterpolation.js

+15-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react'
1+
import React, { Component, PropTypes } from 'react'
22

33
const styled = ([style]) => style.trim()
44
.split(/\s*;\s*/)
@@ -14,21 +14,27 @@ function load() {
1414
];
1515
}
1616

17-
export default class extends React.Component {
18-
constructor(props) {
19-
super(props);
17+
export default class extends Component {
18+
static propTypes = {
19+
notifyRendered: PropTypes.func
20+
}
2021

21-
this.done = () => {};
22-
this.props.setCallWhenDone && this.props.setCallWhenDone((done) => {
23-
this.done = done;
24-
});
22+
static defaultProps = {
23+
notifyRendered: () => {}
24+
}
2525

26+
constructor(props) {
27+
super(props);
2628
this.state = { users: [] };
2729
}
2830

2931
async componentDidMount() {
3032
const users = load();
31-
this.setState({ users }, () => this.done());
33+
this.setState({ users });
34+
}
35+
36+
componentDidUpdate() {
37+
this.props.notifyRendered();
3238
}
3339

3440
render() {

packages/react-scripts/fixtures/kitchensink/src/features/syntax/DefaultParameters.js

+15-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react'
1+
import React, { Component, PropTypes } from 'react'
22

33
function load(id = 0) {
44
return [
@@ -9,21 +9,27 @@ function load(id = 0) {
99
];
1010
}
1111

12-
export default class extends React.Component {
13-
constructor(props) {
14-
super(props);
12+
export default class extends Component {
13+
static propTypes = {
14+
notifyRendered: PropTypes.func
15+
}
1516

16-
this.done = () => {};
17-
this.props.setCallWhenDone && this.props.setCallWhenDone((done) => {
18-
this.done = done;
19-
});
17+
static defaultProps = {
18+
notifyRendered: () => {}
19+
}
2020

21+
constructor(props) {
22+
super(props);
2123
this.state = { users: [] };
2224
}
2325

2426
async componentDidMount() {
2527
const users = load();
26-
this.setState({ users }, () => this.done());
28+
this.setState({ users });
29+
}
30+
31+
componentDidUpdate() {
32+
this.props.notifyRendered();
2733
}
2834

2935
render() {

0 commit comments

Comments
 (0)