Model
View
Model
View
class ActInfo extends Component {
componentDidMount() {
this.fetchActInfo()
this.startTimer()
this.fixWindow()
}
componentWillUnmount() {
this.cancelFetchActInfo()
this.clearTimer()
this.unFixWindow()
}
componentDidUpdate(prevProps) {
if(this.props.aaa !== prevProps.aaa) {
...
}
}
}
view(html css) | interaction render logic | |
---|---|---|
✅ | ✅ | |
✅ | ❔ | |
❔ | ✅ |
stateful Component
stateless component
❔❔❔
Mixins
ComponetA
ComponetB
const MouseMixin = {
getInitialState() {
return { x: 0, y: 0 }
},
handleMouseMove(event) {
this.setState({
x: event.clientX,
y: event.clientY
})
}
}
const App = React.createClass({
mixins: [ MouseMixin ],
render() {
const { x, y } = this.state
return (
<div
style={{ height: '100%' }}
onMouseMove={this.handleMouseMove}
>
<h1>The mouse position is ({x}, {y})</h1>
</div>
)
}
})
const App = React.createClass({
// Use the mixin!
mixins: [
MouseMixin,
toolTipsMixin,
popUpMixin
],
render() {
const { x, y } = this.state
return (
<div
style={{ height: '100%' }}
onMouseMove={this.handleMouseMove}
>
<h1>The mouse position is ({x}, {y})</h1>
</div>
)
}
})
Componnt
Componnt
hoc
hoc
const withFeature = (Cmp) => {
class Wrapper extends Component {
componentDidMount () { ... }
componentWillUnmount () { ... }
componentDidUpdate () { ... }
shareMethod = () => {}
state = {
shareData: 'initial'
}
render() {
<Cmp
data={this.state.shareData}
method={this.shareMethod}
/>
}
}
return Wrapper
}
export default withFeature(Cmp)
react-redux connect
react-i13n createI13n
recompose pure
const ComponentA = ({ doSomeThing }) => (
<div onClick={doSomeThing} />
)
compose(
withRouter
withContext
connect
withI13n
withI18n
)(ComponentA)
const App = () => (
<MouseHover>
{(handleMouseHover, isMouseHover) => (
<div
onMouseHover={handleMouseHover}
className={
isMouseHover ? style.active : style.normal
}
/>
)}
<MouseHover>
)
const App = () => (
<Tooltip>
{(renderToolTip)=> (
<MouseHover>
{(handleMouseHover, isMouseHover) => (
<div
onMouseHover={handleMouseHover}
className={
isMouseHover ? style.active : style.normal
}
>
{renderToolTip()}
</div>
)}
<MouseHover>
)}
</Tooltip>
)
hoc
babel loose compile 3.8KB
minify 2.5KB
0.66%
hooks
babel loose compile 1.4KB
minify 684 Byte
0.48%
😀
😀
😀
😀
😀
😀
😀
😀
😀
let hooks = null;
export function useHook() {
hooks.push(hookData);
}
function reactInternalRenderAComponentMethod(component) {
hooks = [];
component();
let hooksForThisComponent = hooks;
hooks = null;
}
Basic Hooks
Additional Hooks