A React component calls includes or slice on a value that is still undefined
TypeError: Cannot read properties of undefined (reading 'includes')Short answer
The value you call .includes() or .slice() on is undefined at that moment. In React that mostly means the component rendered before the data arrived, which is why it breaks after a refresh but not on navigation. Give the state a safe starting value, or guard the call, and check the data shape.
Fix it fast
1Find out which cause you have
Open the stack trace in the console and find the first line in your own code. Then log the variable on the line above the call.
console.log("items:", items, typeof items);2Match what you see, then jump to its fix
What does the error look like?
TypeError: Cannot read properties of undefined (reading 'includes')TypeError: Cannot read properties of undefined (reading 'slice')
Cause 1 of 2
The state starts as undefined and data arrives later
1Check
Look at the useState call for the value. The first render uses exactly the initial state you passed, and it runs before any fetch finishes; with nothing passed there is nothing to call the method on.
grep -rn "useState()" src/2Fix
Start with a value of the right type, such as an empty array or string, so the method exists on the first render.
const [items, setItems] = useState([]);
const first = items.slice(0, 5);3Verify
Hard refresh the page; it renders with no error and fills when data arrives.
# browser: reload with the console openConfirmed by react.dev
Cause 2 of 2
The value is read from a property that is not always there
1Check
A property read on null or undefined throws. The response may omit the field, or name it differently than your code does.
console.log(JSON.stringify(response, null, 2));2Fix
Use optional chaining to short-circuit, with a fallback, or check explicitly.
const hasTag = user.tags?.includes("admin") ?? false;
const preview = (post.body ?? "").slice(0, 120);3Verify
Run the screen against a record that lacks the field.
# browser: open a record without the field; no errorConfirmed by developer.mozilla.org
How do I stop it coming back?
- Give every piece of state an initial value of its final type.
- Type API responses in TypeScript so a missing field is a compile error.
- Show a loading state while the data is absent instead of rendering the list.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Web apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fixWeb apps
Axios returns 401 from a React app but the login works elsewhere
Error: Request failed with status code 401
A 401 means the server received the request without valid credentials.
3 causes, each with a fixWeb apps
An axios call never finishes when the API server stops answering
code: 'ECONNABORTED'
Axios has no timeout unless you give it one: the default is 0, which means wait forever.
3 causes, each with a fix