Skip to content

Instantly share code, notes, and snippets.

View nirsky's full-sized avatar

Nir Hadassi nirsky

View GitHub Profile
// node-handler.js
export const handle = () => {
require('fs').readFileSync(...);
...
};
// browser-handler.js
export const handle = () => { ... };
// node-handler.js
const fs = require('fs');
export const handle = () => {
fs.readFileSync(...);
...
};
// browser-handler.js
export const handle = () => { ... };
//Class
shouldComponentUpdate(nextProps) {
return nextProps.count !== this.props.count
}
//memo
import React, { memo } from 'react';
const MyComponent = memo(
_MyComponent,
//Class
shouldComponentUpdate(nextProps) {
return nextProps.count !== this.props.count
}
//memo
import React, { memo } from 'react';
const MyComponent = memo(
_MyComponent,
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>;
const Timer = (props) => {
const intervalRef = useRef();
useEffect(() => {
const id = setInterval(() => {
// ...
});
intervalRef.current = id;
return () => {
clearInterval(intervalRef.current);
//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()}>
//Class
componentWillReceiveProps(nextProps) {
if (nextProps.count !== this.props.count) {
console.log('count changed', nextProps.count);
}
}
//Hooks
//Printing 1st iteration:
useEffect(() => {
//Class
componentWillUnmount() {
console.log('I am unmounting');
}
//Hooks
useEffect(() => {
return () => console.log('I am unmounting');
}, [])
//Class
componentDidMount() {
console.log('I just mounted!');
}
//Hooks
useEffect(() => {
console.log('I just mounted!');
}, [])