/
githubmirror
/
preact
Обзор
Документация
Войти
/
githubmirror
/
preact
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
compat/src/memo.js
31 строка
1 KB
jdecroock
Remove straggler forwarded
28 июл 2026, 20:11
28 июл 2026, 20:11
5e1a85f
Код
Авторство
О чём код?
import { createElement } from 'preact'; import { shallowDiffers } from './util'; /** * Memoize a component, so that it only updates when the props actually have * changed. This was previously known as `React.pure`. * @param {import('./internal').FunctionComponent} c functional component * @param {(prev: object, next: object) => boolean} [comparer] Custom equality function * @returns {import('./internal').FunctionComponent} */ export function memo(c, comparer) { function shouldUpdate(nextProps) { const ref = this.props.ref; if (ref != nextProps.ref && ref) { typeof ref == 'function' ? ref(null) : (ref.current = null); } return comparer ? !comparer(this.props, nextProps) || ref != nextProps.ref : shallowDiffers(this.props, nextProps); } function Memoed(props) { this.shouldComponentUpdate = shouldUpdate; return createElement(c, props); } Memoed.displayName = 'Memo(' + (c.displayName || c.name) + ')'; Memoed.prototype.isReactComponent = true; Memoed.type = c; return Memoed; }