Fix Warning: Failed prop type: Invalid prop `count` of type `string` supplied to `StatCard`, expected `number`. in React
This warning fires when a component receives a prop whose type does not match the defined PropTypes or TypeScript interface. The component may render incorrectly or produce subtle bugs. Fix it by passing the correct type from the parent, or by coercing the value at the component boundary and updating the type definitions to match actual usage.
Reading the Stack Trace
Here's what each line means:
- at StatCard (webpack-internal:///./src/components/StatCard.tsx:5:3): StatCard received a string for the count prop but expects a number, causing the PropTypes validation warning.
- at Dashboard (webpack-internal:///./src/pages/Dashboard.tsx:22:7): Dashboard is the parent component passing the wrong type for the count prop to StatCard.
- at renderWithHooks (node_modules/react-dom/cjs/react-dom.development.js:16305:18): React checks PropTypes during rendering in development mode and logs the warning.
Common Causes
1. API returns string instead of number
The API response contains numeric values as strings (e.g., JSON parsed from a database that stores numbers as text), and the component passes them directly without conversion.
export default function Dashboard() {
const [stats, setStats] = useState({ count: '0' });
useEffect(() => {
fetch('/api/stats').then(r => r.json()).then(setStats);
}, []);
return <StatCard count={stats.count} />;
}
2. Form input value used directly
HTML input values are always strings, and passing an input value directly to a component expecting a number triggers the type mismatch.
export default function Settings() {
const [limit, setLimit] = useState('10');
return (
<>
<input value={limit} onChange={e => setLimit(e.target.value)} />
<StatCard count={limit} />
</>
);
}
3. TypeScript type assertion hiding the mismatch
A type assertion (as any or as number) silences TypeScript but the runtime value is still a string, causing the PropTypes warning.
const rawCount = searchParams.get('count') as unknown as number;
return <StatCard count={rawCount} />;
The Fix
Convert the API response value to a number using Number() before storing it in state. This ensures the StatCard always receives the correct type and eliminates the PropTypes warning.
export default function Dashboard() {
const [stats, setStats] = useState({ count: '0' });
useEffect(() => {
fetch('/api/stats').then(r => r.json()).then(setStats);
}, []);
return <StatCard count={stats.count} />;
}
export default function Dashboard() {
const [stats, setStats] = useState({ count: 0 });
useEffect(() => {
fetch('/api/stats')
.then(r => r.json())
.then(data => setStats({ count: Number(data.count) }));
}, []);
return <StatCard count={stats.count} />;
}
Testing the Fix
import { render, screen } from '@testing-library/react';
import StatCard from './StatCard';
describe('StatCard', () => {
it('renders count as a number', () => {
render(<StatCard count={42} />);
expect(screen.getByText('42')).toBeInTheDocument();
});
it('does not trigger prop type warnings', () => {
const spy = jest.spyOn(console, 'error').mockImplementation(() => {});
render(<StatCard count={42} />);
expect(spy).not.toHaveBeenCalledWith(
expect.stringContaining('Failed prop type')
);
spy.mockRestore();
});
it('renders zero correctly', () => {
render(<StatCard count={0} />);
expect(screen.getByText('0')).toBeInTheDocument();
});
});
Run your tests:
npm test
Pushing Through CI/CD
git checkout -b fix/react-prop-types-error,git add src/pages/Dashboard.tsx src/components/StatCard.tsx src/components/__tests__/StatCard.test.tsx,git commit -m "fix: convert API count to number before passing to StatCard",git push origin fix/react-prop-types-error
Your CI config should look something like this:
name: CI
on:
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- run: npm ci
- run: npm test -- --coverage
- run: npm run build
The Full Manual Process: 18 Steps
Here's every step you just went through to fix this one bug:
- Notice the error alert or see it in your monitoring tool
- Open the error dashboard and read the stack trace
- Identify the file and line number from the stack trace
- Open your IDE and navigate to the file
- Read the surrounding code to understand context
- Reproduce the error locally
- Identify the root cause
- Write the fix
- Run the test suite locally
- Fix any failing tests
- Write new tests covering the edge case
- Run the full test suite again
- Create a new git branch
- Commit and push your changes
- Open a pull request
- Wait for code review
- Merge and deploy to production
- Monitor production to confirm the error is resolved
Total time: 30-60 minutes. For one bug.
Or Let bugstack Fix It in Under 2 minutes
Every step above? bugstack does it automatically.
Step 1: Install the SDK
npm install bugstack-sdk
Step 2: Initialize
import { initBugStack } from 'bugstack-sdk'
initBugStack({ apiKey: process.env.BUGSTACK_API_KEY })
Step 3: There is no step 3.
bugstack handles everything from here:
- Captures the stack trace and request context
- Pulls the relevant source files from your GitHub repo
- Analyzes the error and understands the code context
- Generates a minimal, verified fix
- Runs your existing test suite
- Pushes through your CI/CD pipeline
- Deploys to production (or opens a PR for review)
Time from error to fix deployed: Under 2 minutes.
Human involvement: zero.
Try bugstack Free →No credit card. 5-minute setup. Cancel anytime.
Deploying the Fix (Manual Path)
- Run the test suite locally to confirm no prop type warnings appear.
- Open a pull request with the type conversion fix.
- Wait for CI checks to pass on the PR.
- Have a teammate review and approve the PR.
- Merge to main and verify the component renders correctly in staging.
Frequently Asked Questions
BugStack validates that all props match their declared types, runs your test suite with strict prop type checking, and confirms no downstream components are affected before marking it safe.
BugStack never pushes directly to production. Every fix goes through a pull request with full CI checks, so your team can review it before merging.
TypeScript catches type mismatches at compile time, which is more reliable than runtime PropTypes checks. If you are using TypeScript, PropTypes are redundant for type safety but can still help with third-party consumer documentation.
PropTypes checks are stripped in production builds, so the warning disappears. However, the underlying bug remains, potentially causing NaN calculations or incorrect comparisons.