JIYIK CN >

Current Location:Home > Learning > WEB FRONT-END > React >

TypeError: 'XXX' is not a function error fix in React

Author:JIYIK Last Updated:2025/03/15 Views:

React.js “Uncaught TypeError: X is not a function” occurs when we try to call a value that is not a function as a function, such as calling the props object instead of a function. To fix this error, use console.log to print the value being called to confirm whether it is a function.

React TypeError setCount is not a function

The following is an example of how the error occurs.

import {useState} from 'react';

// 👇️ should take props object and not setCount directly
// ⛔️ Uncaught TypeError: setCount is not a function
function Child(setCount) {
  return (
    <div>
      <button onClick={() => setCount(current => current + 1)}>
        Click
      </button>
    </div>
  );
}

export default function App() {
  const [count, setCount] = useState(0);

  return (
    <div>
      <Child setCount={setCount} />

      <h2>Count: {count}</h2>
    </div>
  );
}

ChildThe component should get the props object, but we named it setCount and tried to call the props object in the component.

The best way to fix the error is to log the setCount value in the Child component and make sure it is a function.

ChildsetCountComponents should accept a props object, and should access functions on the props .

import {useState} from 'react';

// 👇️ 现在组件接受 props 对象并调用 props.setCount 函数
function Child(props) {
  console.log('props obj:', props);
  return (
    <div>
      <button onClick={() => props.setCount(current => current + 1)}>
        Click
      </button>
    </div>
  );
}

export default function App() {
  const [count, setCount] = useState(0);

  return (
    <div>
      <Child setCount={setCount} />

      <h2>Count: {count}</h2>
    </div>
  );
}

Alternatively, we can deconstruct the properties in the function definition.

// 👇️ 解构 setCount 属性并直接使用它
function Child({setCount}) {
  return (
    <div>
      <button onClick={() => setCount(current => current + 1)}>Click</button>
    </div>
  );
}

export default function App() {
  const [count, setCount] = useState(0);

  return (
    <div>
      <Child setCount={setCount} />

      <h2>Count: {count}</h2>
    </div>
  );
}

Another common reason for “Uncaught TypeError: XXX is not a function” error in React is when we try to call a function on a value of the incorrect type.

For example, if we try to call the method on a value that is not an array pop(), an error will be generated.

const obj = {};

// ⛔️ Uncaught TypeError: obj.pop is not a function
obj.pop();

The pop() method can only be called on arrays, so trying to call it on any other type will throw an "X is not a function" error because there is no pop function on that type.

The best way to debug this is to use console.logprint_value to print the value you call the function with and make sure it is the correct type.

const obj = {};

console.log(typeof obj); // 👉️ "object"
console.log(Array.isArray(obj)); // 👉️ false

Once it is determined that the value on which the method was called is of the expected type and that the method is supported by said type, the error will be resolved.

We can also use conditionals to check if a value is of the correct type before calling a method.

const obj = {};

if (Array.isArray(obj)) {
  console.log(obj.pop());
}

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

React Tutorial - Transferring Props

Publish Date:2025/03/16 Views:188 Category:React

React transfers Props. Props are generated when components are encapsulated. Components expose some properties (Props) to the outside world to complete some functions.

React Tutorial: Props Anti-Pattern

Publish Date:2025/03/16 Views:187 Category:React

React's Props anti-pattern, using Props to generate state in getInitialState is an anti-pattern - Anti-Pattern.

React Tutorial - Props Validation

Publish Date:2025/03/16 Views:102 Category:React

Props validation is a very useful way to use components correctly. It can avoid many bugs and problems as your application becomes more and more complex. In addition, it can make your program more readable.

Why do you need to bind event handlers in React Class Components?

Publish Date:2025/03/16 Views:60 Category:React

When using React, we must have come across control components and event handlers. We need to use `.bind()` in the constructor of the custom component to bind these methods to the component instance. As shown in the following code:

Solution to the error "does not contain a default export" in React

Publish Date:2025/03/16 Views:191 Category:React

When we try to use `default import` to import from a module that does not have a `default export`, we get a "does not contain a default export" error. To fix the error, make sure the module has named exports and wrap the import in curly braces, e.g.

Solve the Module not found: Can't resolve 'react-bootstrap' error

Publish Date:2025/03/16 Views:90 Category:React

To resolve the error "Module not found: Error: Can't resolve 'react-bootstrap'", make sure to install the react-bootstrap package by opening a terminal in the root directory of the project and running the command `npm install react-bootstrap bootstrap

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial