陳冠霖
Jason Chen
Software engineer in
Yahoo Taiwan EC
slack: gchen02
https://fb.me/Jason.GuanLin.Chen
https://medium.com/@as790726
Comics Scroller Chrome extension
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) {
...
}
}
}
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>
)
}
})
Dan Abramov in react official blog
const ComponentA = ({ doSomeThing }) => (
<div onClick={doSomeThing} />
)
compose(
withRouter
withContext
connect
withI13n
withI18n
)(ComponentA)
const App = () => (
<Tooltip>
{(renderToolTip) => (
<MouseHover>
{(handleMouseHover, isMouseHover) => (
<div
onMouseHover={handleMouseHover}
className={
isMouseHover ? style.active : style.normal
}
>
{renderToolTip()}
</div>
)}
<MouseHover>
)}
</Tooltip>
)
const Cmp = () => {
const [state, setState] = useState(initialValue)
return (
<div onClick={() => setState(value)}>
{state}
</div>
)
}
const Cmp = () => {
useEffect(() => {
const timeout = setTimeout(() => {}, 10000)
return () => clearTimeout(timeout)
})
return (
<div>
{state}
</div>
)
}
hoc
babel loose compile 3.8KB
minify 2.5KB
0.66%
hooks
babel loose compile 1.4KB
minify 684 Byte
0.48%
😀
😀
😀
😀
😀
😀
😀
😀
Basic Hooks
Additional Hooks
let hooks = null;
export function useHook() {
hooks.push(hookData);
}
function reactInternalRenderAComponentMethod(component) {
hooks = [];
component();
let hooksForThisComponent = hooks;
hooks = null;
}
State 1
setter 1
State 2
setter 2
State 3
setter 3
useEffect(() => {
}, [listenOuter1, listenOuter2])
const memoizedCallback = useCallback(() => {
}, [listenOuter1, listenOuter2])
❌
✅
🤔 receive props
🤔 pure render
🤔 no direct instantiation
🤔 local mutable state
class Item extends Component {
state = { isOn: false }
}
const Item = ({ text }) => (
<div>{text}</div>
)