[📖] explain uncontrolled vs controlled components
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- Half a day
- Newbie friendliness
- 35/100
- Issue type
- Documentation
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- react, typescript
- Domain
- documentation, frontend
Research direction
Start with the proposed /guides/react-cheat-sheet location and review the supplied controlled and uncontrolled examples. Add a fake reusable-component implementation, improve the wording, and clarify the Qwik-versus-React API rationale only where supported. The work is done when the guide clearly explains the patterns and examples are consistent.
Written by the indexing model from the issue text.
Description
Suggestion
Perhaps tbd in /guides/react-cheat-sheet
Uncontrolled vs controlled
In Qwik, the value prop by default behaves like the vanilla html value attribute. So the pattern is the following:
//uncontrolled -> behaves like vanilla html
<input type="checkbox" checked={true} />
//controlled (one way data-binding)
const acceptConditions = useSignal(false);
<input type="checkbox" checked={acceptConditions.value} onChange$={(_, el) => acceptConditions.value = el.checked } />
//controlled (two way data-binding = syntactic sugar to achieve above behavior)
const acceptConditions = useSignal(false);
<input type="checkbox" bind:checked={acceptConditions} />
React provides defaultValue, defaultOpen, defaultToggled, etc. props to enable "uncontrolled" inputs for when you want your inputs to work with vanilla html forms. This is because if you just set a value attribute like in vanilla html it will be interpreted as a controlled component by React and won't work. You have to also set an onChange event handler to express how to update the state the other way around.
The React pattern is the following:
//uncontrolled (this is different from native HTML)
<input type="checkbox" defaultChecked={true} />
//controlled (one way data-binding)
const [acceptConditions, setAcceptConditions] = useState(false);
<input type="checkbox" checked={acceptConditions} onChange$={(e) => setAcceptConditions(e.target.checked)} />
//controlled (two way data-binding)
There are no "two way data-binding" in React 🤪
Consistency recommendation for reusable components:
To keep your APIs consistent in Qwik Land, we recommend using the Qwik input patterns on your reusable components. For example:
//uncontrolled
<ComponentRoot open={true} />
//controlled (one way data-binding)
const isOpen = useSignal<boolean>(true);
<ComponentRoot open={isOpen.value} onChange${() => isOpen.value = !isOpen.value} />
//controlled (two way data-binding)
const isOpen = useSignal<boolean>(true);
<ComponentRoot bind:open={isOpen} />
TODO:
- add an example implementation with a fake reusable component
- Maybe explain why we can keep the API similar to native HTML in Qwik while React can't? Is it because of signals? I'm not sure..
- Improve the wording and sentences structure
- Dominant language
- TypeScript
- Stars
- 22.1k
- Forks
- 1.4k
- Avg merge
- 1d 17h
- Merged PRs (30d)
- 98
Getting set up
Starts the project's dev container in your browser, under your own GitHub account.
- Ships a Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from QwikDev/qwik
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Maintainers usually reply within 1 day
-
docs router V2
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 1 day
-
DX enhancement V2
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
QwikDev/qwik#8480 · 2 comments ·
Maintainers usually reply within 1 day
-
[🐞] Production build hoists a nested `.map()` handler's captures out of both loops, so SSR failsOpen
Difficulty 4/5 3-5 days Newbie friendliness 25/100
QwikDev/qwik#9190 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 4/5 3-5 days Newbie friendliness 22/100
Maintainers usually reply within 1 day
Similar issues
-
area: backend bug priority: low
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
snapotter-hq/SnapOtter#2254 ·
Maintainers usually reply within 1 day
-
bug ticket
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
cratestack/cratestack#1154 ·
Maintainers usually reply within 1 day
-
server 消息处理器 cmd 分支补显式错误回报——竞态非法命令现走未处理拒绝Possibly taken @openaddr claimed this today. Openready-for-agent refactor wayfinder:task
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
openaddr/dafung-web#428 ·
Maintainers usually reply within 1 day
-
Flaky: mongodb-memory-server 'Port already in use' when another process starts a mongod concurrentlyOpenarea:testing bug effort:S priority:P2
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
Maintainers usually reply within 1 day
-
lens:agent lens:process process
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
thebristolsound/birdbrain#1772 ·
Maintainers usually reply within 1 day