This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// node-handler.js | |
export const handle = () => { | |
require('fs').readFileSync(...); | |
... | |
}; | |
// browser-handler.js | |
export const handle = () => { ... }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// node-handler.js | |
const fs = require('fs'); | |
export const handle = () => { | |
fs.readFileSync(...); | |
... | |
}; | |
// browser-handler.js | |
export const handle = () => { ... }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Class | |
shouldComponentUpdate(nextProps) { | |
return nextProps.count !== this.props.count | |
} | |
//memo | |
import React, { memo } from 'react'; | |
const MyComponent = memo( | |
_MyComponent, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Class | |
shouldComponentUpdate(nextProps) { | |
return nextProps.count !== this.props.count | |
} | |
//memo | |
import React, { memo } from 'react'; | |
const MyComponent = memo( | |
_MyComponent, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Counter = props => { | |
const [count, setCount] = useState(0); | |
const prevCountRef = useRef(); | |
useEffect(() => { | |
prevCountRef.current = count; | |
}); | |
const prevCount = prevCountRef.current; | |
return <h1>Now: {count}, before: {prevCount}</h1>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Timer = (props) => { | |
const intervalRef = useRef(); | |
useEffect(() => { | |
const id = setInterval(() => { | |
// ... | |
}); | |
intervalRef.current = id; | |
return () => { | |
clearInterval(intervalRef.current); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Class | |
class InputWithFocus extends React.Component { | |
constructor() { | |
super(); | |
this.inputRef = null; | |
} | |
render() { | |
return <div> | |
<input ref={inputRef => { this.inputRef = inputRef }} /> | |
<button onClick={() => this.inputRef.focus()}> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Class | |
componentWillReceiveProps(nextProps) { | |
if (nextProps.count !== this.props.count) { | |
console.log('count changed', nextProps.count); | |
} | |
} | |
//Hooks | |
//Printing 1st iteration: | |
useEffect(() => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Class | |
componentWillUnmount() { | |
console.log('I am unmounting'); | |
} | |
//Hooks | |
useEffect(() => { | |
return () => console.log('I am unmounting'); | |
}, []) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Class | |
componentDidMount() { | |
console.log('I just mounted!'); | |
} | |
//Hooks | |
useEffect(() => { | |
console.log('I just mounted!'); | |
}, []) |