Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

A React component calls includes or slice on a value that is still undefined

ErrorTypeError: 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.

  • Applies to React 19, Node.js 22, Chrome stable
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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);
  2. 2Match what you see, then jump to its fix

    1. 1The page works after clicking around but crashes on a hard refreshThe state starts as undefined and data arrives laterFix
    2. 2The error appears for some records or API responses onlyThe value is read from a property that is not always thereFix

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 open

Confirmed by react.dev

Did this fix it?

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 error

Confirmed by developer.mozilla.org

Did this fix it?

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:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.