From be1f3b84884d0dfa9dba602bf0233179c2d179b6 Mon Sep 17 00:00:00 2001 From: Chris Cowan Date: Fri, 26 Apr 2024 16:07:14 -0700 Subject: [PATCH] use native JS private properties --- README.md | 10 +++++----- src/index.js | 10 +++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 9569bef..12d85f8 100644 --- a/README.md +++ b/README.md @@ -10,26 +10,26 @@ import React from 'react'; import MultiRef from 'react-multi-ref'; class Foo extends React.Component { - _itemRefs = new MultiRef(); + #itemRefs = new MultiRef(); render() { // Make a 5-item array of divs with keys 0,1,2,3,4 const items = new Array(5).fill(null).map((n, i) =>
- +
); return (
- + { items }
); } - _onClick = () => { + #onClick = () => { const parts = []; - this._itemRefs.map.forEach(input => { + this.#itemRefs.map.forEach(input => { parts.push(input.value) }); alert('all inputs: ' + parts.join(', ')); diff --git a/src/index.js b/src/index.js index a1d4fea..fa367e2 100644 --- a/src/index.js +++ b/src/index.js @@ -5,10 +5,10 @@ type RefFn = (value: V|null) => mixed; export default class MultiRef { map: Map = new Map(); - _refFns: Map> = new Map(); + #refFns: Map> = new Map(); ref(key: K): RefFn { - const refFn = this._refFns.get(key); + const refFn = this.#refFns.get(key); if (refFn) { return refFn; } else { @@ -16,14 +16,14 @@ export default class MultiRef { if (value == null) { // Support for React <=18, which cleans up ref functions by calling them // with null. - this._refFns.delete(key); + this.#refFns.delete(key); this.map.delete(key); } else { - this._refFns.set(key, refFn); + this.#refFns.set(key, refFn); this.map.set(key, value); // React 19+ cleanup support return () => { - this._refFns.delete(key); + this.#refFns.delete(key); this.map.delete(key); }; }