React 18 String Refs Migration

React 18 String Refs Migration

Gives exact patterns for migrating React string refs to createRef in class components, including multiple refs, lists, callbacks and forwarded refs.

Skill files2 files

references/

SKILL.md41 lines
1
---
2
name: react18-string-refs
3
description: 'Provides exact migration patterns for React string refs (ref="name" + this.refs.name) to React.createRef() in class components. Use this skill whenever migrating string ref usage - including single element refs, multiple refs in a component, refs in lists, callback refs, and refs passed to child components. Always use this skill before writing any ref migration code - the multiple-refs-in-list pattern is particularly tricky and this skill prevents the most common mistakes. Use it for React 18.3.1 migration (string refs warn) and React 19 migration (string refs removed).'
4
---
5
6
# React 18 String Refs Migration
7
8
String refs (`ref="myInput"` + `this.refs.myInput`) were deprecated in React 16.3, warn in React 18.3.1, and are **removed in React 19**.
9
10
## Quick Pattern Map
11
12
| Pattern | Reference |
13
|---|---|
14
| Single ref on a DOM element | [→ patterns.md#single-ref](references/patterns.md#single-ref) |
15
| Multiple refs in one component | [→ patterns.md#multiple-refs](references/patterns.md#multiple-refs) |
16
| Refs in a list / dynamic refs | [→ patterns.md#list-refs](references/patterns.md#list-refs) |
17
| Callback refs (alternative approach) | [→ patterns.md#callback-refs](references/patterns.md#callback-refs) |
18
| Ref passed to a child component | [→ patterns.md#forwarded-refs](references/patterns.md#forwarded-refs) |
19
20
## Scan Command
21
22
```bash
23
# Find all string ref assignments in JSX
24
grep -rn 'ref="' src/ --include="*.js" --include="*.jsx" | grep -v "\.test\."
25
26
# Find all this.refs accessors
27
grep -rn "this\.refs\." src/ --include="*.js" --include="*.jsx" | grep -v "\.test\."
28
```
29
30
Both should be migrated together - find the `ref="name"` and the `this.refs.name` accesses for each component as a pair.
31
32
## The Migration Rule
33
34
Every string ref migrates to `React.createRef()`:
35
36
1. Add `refName = React.createRef();` as a class field (or in constructor)
37
2. Replace `ref="refName"` → `ref={this.refName}` in JSX
38
3. Replace `this.refs.refName` → `this.refName.current` everywhere
39
40
Read `references/patterns.md` for the full before/after for each case.
41

© GitHub, Inc. · MIT · source · posted by @OpenSourceBuffGuy on promptparty.com

@OpenSourceBuffGuy

I find openly licensed prompts and post them here.

0 followers · 14,621 prompts

More by @OpenSourceBuffGuy

See all
Sea turtle carrying floating film studio island prompt image

Sea turtle carrying floating film studio island

Draws a giant sea turtle carrying a floating island film studio through a starry sky, where fairies polish rough film strips into golden film.

image-prompt, nano-banana, 3d-render, cartoon, fantasy-world, creatures, animals, whimsical, dreamy, cinematic, dramatic-lighting, vibrant-colors, bokeh, miniature, widescreen, night-scene, illustration, concept-art, surreal, storytelling, clay-3d

Desert buggy escapes drones past industrial outpost prompt image

Desert buggy escapes drones past industrial outpost

Builds a 15-second desert chase video where a racer in an armored buggy escapes drones past an industrial outpost, using five reference images with set jobs.

video-prompt, hailuo, action, multi-shot, timed-beats, camera-movement, reference-image, character-consistency, negative-prompt, cinematic, sci-fi, cars, landscape, aerial-view, tracking-shot, photorealistic, sound-effects, structured-output, long-form, widescreen

1 view
Cozy Pixel Art Farm Island Village prompt image

Cozy Pixel Art Farm Island Village

Draws a cozy 16-bit pixel-art farming village across six small islands joined by bridges, with exact counts for buildings, animals and crops.

image-prompt, gpt-image, pixel-art, isometric, game-assets, concept-art, maps, worldbuilding, cozy, vibrant-colors, ocean, landscape, wide-shot, widescreen, game-development, video-games, structured-output, whimsical, summer, high-angle, aerial-view, animals, negative-prompt, cityscape

1 view

Reviews

Loading reviews…

Discussion

Loading comments…