Skip to content
Myr

fix (jsx/dom): keep ref.current value during lifecycle. (#2307)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix (jsx/dom): keep ref.current value during lifecycle. * chore: denoify

authored by Taku Amano · committed by GitHub · parent 1511364 · Browse files · 3 files+94 added−46 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+94 added−46 removed

  • tests:+72 added−44 removed
  • other:+22 added−2 removed

3 files+94 added−46 removed

modified:deno_dist/jsx/hooks/index.ts+11 added-1 removed
7 unmodified lines
8export const STASH_EFFECT = 19const STASH_CALLBACK = 210const STASH_USE = 311const STASH_MEMO = 412const STASH_REF = 51314export type EffectData = [15 readonly unknown[] | undefined, // deps16 (() => void | (() => void)) | undefined, // layout effect
246 unmodified lines
263}264265export type RefObject<T> = { current: T | null }266export const useRef = <T>(initialValue: T | null): RefObject<T> => {267 const buildData = buildDataStack.at(-1) as [unknown, NodeObject]268 if (!buildData) {266 return { current: initialValue }269 return { current: initialValue }270 }271 const [, node] = buildData272273 const refArray = (node[DOM_STASH][1][STASH_REF] ||= [])274 const hookIndex = node[DOM_STASH][0]++275276 return (refArray[hookIndex] ||= { current: initialValue })277}278279export const use = <T>(promise: Promise<T>): T => {280 const cachedRes = resolvedPromiseValueMap.get(promise) as [T] | [undefined, unknown] | undefined
56 unmodified lines
modified:src/jsx/dom/index.test.tsx+72 added-44 removed
917 unmodified lines
918 expect(root.innerHTML).toBe('<div><div><p>Count: 1</p></div><button>+</button></div>')919 expect(renderCount).toBe(2)920 })921922 describe('useRef', async () => {922 it('useRef', async () => {923 const Input = ({ label, ref }: { label: string; ref: RefObject<HTMLInputElement> }) => {924 return (925 <div>926 <label>{label}</label>927 <input ref={ref} />928 </div>923 it('simple', async () => {924 const Input = ({ label, ref }: { label: string; ref: RefObject<HTMLInputElement> }) => {925 return (926 <div>927 <label>{label}</label>928 <input ref={ref} />929 </div>930 )931 }932 const Form = () => {933 const [values, setValues] = useState<{ [key: string]: string }>({})934 const nameRef = useRef<HTMLInputElement>(null)935 const emailRef = useRef<HTMLInputElement>(null)936 return (937 <form>938 <Input label='Name' ref={nameRef} />939 <Input label='Email' ref={emailRef} />940 <button941 onClick={(ev: Event) => {942 ev.preventDefault()943 setValues({944 name: nameRef.current?.value || '',945 email: emailRef.current?.value || '',946 })947 }}948 >949 serialize950 </button>951 <span>{JSON.stringify(values)}</span>952 </form>953 )954 }955 const app = <Form />956 render(app, root)957 expect(root.innerHTML).toBe(958 '<form><div><label>Name</label><input></div><div><label>Email</label><input></div><button>serialize</button><span>{}</span></form>'959 )930 }931 const Form = () => {932 const [values, setValues] = useState<{ [key: string]: string }>({})933 const nameRef = useRef<HTMLInputElement>(null)934 const emailRef = useRef<HTMLInputElement>(null)935 return (936 <form>937 <Input label='Name' ref={nameRef} />938 <Input label='Email' ref={emailRef} />939 <button940 onClick={(ev: Event) => {941 ev.preventDefault()942 setValues({943 name: nameRef.current?.value || '',944 email: emailRef.current?.value || '',945 })946 }}947 >948 serialize949 </button>950 <span>{JSON.stringify(values)}</span>951 </form>960 const [nameInput, emailInput] = root.querySelectorAll('input')961 nameInput.value = 'John'962 emailInput.value = 'john@example.com'963 const [button] = root.querySelectorAll('button')964 button.click()965 await Promise.resolve()966 expect(root.innerHTML).toBe(967 '<form><div><label>Name</label><input></div><div><label>Email</label><input></div><button>serialize</button><span>{"name":"John","email":"john@example.com"}</span></form>'968 )953 }954 const app = <Form />955 render(app, root)956 expect(root.innerHTML).toBe(957 '<form><div><label>Name</label><input></div><div><label>Email</label><input></div><button>serialize</button><span>{}</span></form>'958 )959 const [nameInput, emailInput] = root.querySelectorAll('input')960 nameInput.value = 'John'961 emailInput.value = 'john@example.com'962 const [button] = root.querySelectorAll('button')963 button.click()964 await Promise.resolve()965 expect(root.innerHTML).toBe(966 '<form><div><label>Name</label><input></div><div><label>Email</label><input></div><button>serialize</button><span>{"name":"John","email":"john@example.com"}</span></form>'967 )969 })970971 it('update current', async () => {972 const App = () => {973 const [, setState] = useState(0)974 const ref = useRef<boolean>(false)975 return (976 <>977 <button978 onClick={() => {979 setState((c) => c + 1)980 ref.current = true981 }}982 >983 update984 </button>985 <span>{String(ref.current)}</span>986 </>987 )988 }989 const app = <App />990 render(app, root)991 expect(root.innerHTML).toBe('<button>update</button><span>false</span>')992 root.querySelector('button')?.click()993 await Promise.resolve()994 expect(root.innerHTML).toBe('<button>update</button><span>true</span>')995 })996 })997998 describe('useEffect', () => {999 it('simple', async () => {
394 unmodified lines
modified:src/jsx/hooks/index.ts+11 added-1 removed
7 unmodified lines
8export const STASH_EFFECT = 19const STASH_CALLBACK = 210const STASH_USE = 311const STASH_MEMO = 412const STASH_REF = 51314export type EffectData = [15 readonly unknown[] | undefined, // deps16 (() => void | (() => void)) | undefined, // layout effect
246 unmodified lines
263}264265export type RefObject<T> = { current: T | null }266export const useRef = <T>(initialValue: T | null): RefObject<T> => {267 const buildData = buildDataStack.at(-1) as [unknown, NodeObject]268 if (!buildData) {266 return { current: initialValue }269 return { current: initialValue }270 }271 const [, node] = buildData272273 const refArray = (node[DOM_STASH][1][STASH_REF] ||= [])274 const hookIndex = node[DOM_STASH][0]++275276 return (refArray[hookIndex] ||= { current: initialValue })277}278279export const use = <T>(promise: Promise<T>): T => {280 const cachedRes = resolvedPromiseValueMap.get(promise) as [T] | [undefined, unknown] | undefined
56 unmodified lines