-
Notifications
You must be signed in to change notification settings - Fork 294
Class transform with property initializer #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 10 commits
2a509a6
eacb1e1
2c5b461
6d9e3cb
00790d4
93e5931
a151336
20aafc3
c2bda71
f5f6da0
f70dad2
c136e9c
311f472
747dc5b
c7136e2
d4faea4
cb88509
011371d
c4c3c1a
3936f55
ada3fb8
0cfad0a
84f8b71
5f7b1b2
a7f71a3
231f629
b8bb77a
e00677f
59e3671
4f64cc2
d67b6a8
3cc6d71
f06bcd6
53730d9
19b611a
b86a89d
595bb97
5b058f8
b532b52
da84faf
7214e92
bb15c72
9f1702b
d7083fe
ef23055
46d13c0
82d4a52
f00fbb0
667e592
8b15535
953386a
3e62530
3f700c1
51494ac
ab1e1a7
62978f4
ee36b61
1011552
847de6d
b65424c
738558c
3956765
c39d3f7
c02901c
1e9204b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
'use strict'; | ||
|
||
var React = require('React'); | ||
var ReactComponentWithPureRenderMixin = require('ReactComponentWithPureRenderMixin'); | ||
var FooBarMixin = require('FooBarMixin'); | ||
|
||
var ComponentWithNonSimpleInitialState = React.createClass({ | ||
statics: { | ||
iDontKnowWhyYouNeedThis: true, // but comment it | ||
foo: 'bar', | ||
}, | ||
|
||
getInitialState: function() { | ||
return { | ||
counter: this.props.initialNumber + 1, | ||
}; | ||
}, | ||
|
||
render: function() { | ||
return ( | ||
<div>{this.state.counter}</div> | ||
); | ||
}, | ||
}); | ||
|
||
// Comment | ||
module.exports = React.createClass({ | ||
propTypes: { | ||
foo: React.PropTypes.bool, | ||
}, | ||
|
||
getDefaultProps: function() { | ||
return { | ||
foo: 12, | ||
}; | ||
}, | ||
|
||
getInitialState: function() { // non-simple | ||
var data = 'bar'; | ||
return { | ||
bar: data, | ||
}; | ||
}, | ||
|
||
render: function() { | ||
return <div />; | ||
}, | ||
}); | ||
|
||
var ComponentWithOnlyPureRenderMixin = React.createClass({ | ||
mixins: [ReactComponentWithPureRenderMixin], | ||
|
||
getInitialState: function() { | ||
return { | ||
counter: this.props.initialNumber + 1, | ||
}; | ||
}, | ||
|
||
render: function() { | ||
return ( | ||
<div>{this.state.counter}</div> | ||
); | ||
}, | ||
}); | ||
|
||
var ComponentWithInconvertibleMixins = React.createClass({ | ||
mixins: [ReactComponentWithPureRenderMixin, FooBarMixin], | ||
|
||
getInitialState: function() { | ||
return { | ||
counter: this.props.initialNumber + 1, | ||
}; | ||
}, | ||
|
||
render: function() { | ||
return ( | ||
<div>{this.state.counter}</div> | ||
); | ||
}, | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,78 @@ | ||
'use strict'; | ||
|
||
var React = require('React'); | ||
var ReactComponentWithPureRenderMixin = require('ReactComponentWithPureRenderMixin'); | ||
var FooBarMixin = require('FooBarMixin'); | ||
|
||
class ComponentWithNonSimpleInitialState extends React.Component { | ||
static iDontKnowWhyYouNeedThis = true; // but comment it | ||
static foo = 'bar'; | ||
|
||
// Comment | ||
module.exports = class extends React.Component { | ||
constructor(props, context) { | ||
super(props, context); | ||
|
||
this.state = { | ||
foo: 'bar', | ||
counter: props.initialNumber + 1, | ||
}; | ||
} | ||
|
||
render() { | ||
return ( | ||
<div>{this.state.counter}</div> | ||
); | ||
} | ||
} | ||
|
||
// Comment | ||
module.exports = class extends React.Component { | ||
static propTypes = { | ||
foo: React.PropTypes.bool, | ||
}; | ||
|
||
static defaultProps = { | ||
foo: 12, | ||
}; | ||
|
||
state = function() { // non-simple | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn’t look very good. I think most people will be confused by this, and will copy and paste this pattern even when it is unnecessary without understanding what it means. Let’s embed non-trivial state initializers into the constructor. |
||
var data = 'bar'; | ||
return { | ||
bar: data, | ||
}; | ||
}(); | ||
|
||
render() { | ||
return <div />; | ||
} | ||
}; | ||
|
||
module.exports.propTypes = { | ||
foo: React.PropTypes.bool, | ||
}; | ||
class ComponentWithOnlyPureRenderMixin extends React.PureComponent { | ||
constructor(props, context) { | ||
super(props, context); | ||
|
||
this.state = { | ||
counter: props.initialNumber + 1, | ||
}; | ||
} | ||
|
||
render() { | ||
return ( | ||
<div>{this.state.counter}</div> | ||
); | ||
} | ||
} | ||
|
||
var ComponentWithInconvertibleMixins = React.createClass({ | ||
mixins: [ReactComponentWithPureRenderMixin, FooBarMixin], | ||
|
||
getInitialState: function() { | ||
return { | ||
counter: this.props.initialNumber + 1, | ||
}; | ||
}, | ||
|
||
render: function() { | ||
return ( | ||
<div>{this.state.counter}</div> | ||
); | ||
}, | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don’t forget to remove this TODO 😉