慣性聚合 高效追讀感興趣之博客、新聞、科技資訊
閱原文 以慣性聚合開啟

推薦訂閱源

小众软件
小众软件
量子位
阮一峰的网络日志
阮一峰的网络日志
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
美团技术团队
J
Java Code Geeks
Apple Machine Learning Research
Apple Machine Learning Research
腾讯CDC
V
Visual Studio Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 三生石上(FineUI控件)
IT之家
IT之家
博客园 - 【当耐特】
L
LangChain Blog
A
About on SuperTechFans
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
N
Netflix TechBlog - Medium
博客园_首页
WordPress大学
WordPress大学
博客园 - Franky
Engineering at Meta
Engineering at Meta
C
Check Point Blog
aimingoo的专栏
aimingoo的专栏
M
MIT News - Artificial intelligence

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
The "Disappearing Zero": Handling Numeric Inputs in React...
Greg · 2026-05-24 · via DEV Community

If you’ve spent any time building forms in React Native with React Hook Form and validation libraries like Zod or Yup, you’ve likely encountered a strange phenomenon: the "Disappearing Zero."

One minute you're building a sleek checkout or progress flow, and the next, your users are complaining that every time they try to enter 0, the input field just... wipes itself clean.

The culprit? JavaScript’s definition of "falsy."


The Trap: JavaScript Falsiness

In React Native, numeric inputs often start as null or undefined (or a number type in your state). Since TextInput (or custom Input components) expect a string, a common pattern is to cast the value like this:

// ❌ The Buggy Way
value={value ? String(value) : ''}

Enter fullscreen mode Exit fullscreen mode

On the surface, this looks clean. If there's a value, stringify it; otherwise, show an empty string.

The Gotcha: In JavaScript, 0 is falsy.

When a user types "0", the expression value ? ... evaluates to false, and the input receives an empty string (''). The zero vanishes instantly, leaving your users confused and your validation library potentially complaining about a missing value.


The Solution: Explicit Checks

To fix this, we need to stop relying on loose truthiness and start checking for what we actually care about: whether the value is null or undefined.

// ✅ The Robust Way
value={value !== null && value !== undefined ? String(value) : ''}

Enter fullscreen mode Exit fullscreen mode

By being explicit, we ensure that 0 (which is not null or undefined) is correctly stringified and rendered in the UI.


Real-World Example: React Hook Form + Controller

Here is how this looks in a typical implementation. In this example, we're tracking "Completed Stages," where 0 is a perfectly valid (and common) input.

<Controller
  control={control}
  name="completedStages"
  render={({ field: { onChange, onBlur, value } }) => (
    <Input
      label="Completed Stages"
      // The Fix: Ensure 0 is correctly rendered as a string
      value={value !== null && value !== undefined ? String(value) : ''}
      onChangeText={(text) => {
        // Convert back to number for your validation schema (Zod/Yup)
        const parsed = parseInt(text, 10);
        onChange(isNaN(parsed) ? undefined : parsed);
      }}
      onBlur={onBlur}
      keyboardType="number-pad"
      placeholder="5"
      error={errors.completedStages?.message}
    />
  )}
/>

Enter fullscreen mode Exit fullscreen mode


Why This Matters for Zod and Yup

Validation libraries like Zod and Yup are strict about types. If your UI logic converts a 0 into an empty string (''), your schema validation might fail with a "Required" error or a type mismatch, even though the user intended to enter zero.

By fixing the UI representation, you keep your data flow consistent:

  1. User enters 0 -> UI sees "0".
  2. onChange parses "0" -> Hook Form stores 0.
  3. Zod/Yup validates 0 -> Success!

Summary

In React Native forms, truthiness is often too blunt a tool for numeric inputs. When handling the value prop:

  • Avoid value ? String(value) : ''
  • Prefer value !== null && value !== undefined ? String(value) : ''

It’s a tiny change that prevents one of the most common (and annoying) bugs in mobile form development.