React Typescript: The specified argument type cannot be assigned to the parameter type

Creating a Check Box Button React component has resulted in an error related to setRSelected(1) and setRSelected(2)...

  const [cSelected, setCSelected] = useState([]);
  const [rSelected, setRSelected] = useState();

  const onCheckboxBtnClick = (selected: never) => {
    const index = cSelected.indexOf(selected);
    if (index < 0) {
      cSelected.push(selected);
    } else {
      cSelected.splice(index, 1);
    }
    setCSelected([...cSelected]);
  }
  return (
    <div>
      <h5>Radio Buttons</h5>
      <ButtonGroup>
        <Button color="primary" onClick={() => setRSelected(1)} active={rSelected === 1}>One</Button>
        <Button color="primary" onClick={() => setRSelected(2)} active={rSelected === 2}>Two</Button>
        <Button color="primary" onClick={() => setRSelected(3)} active={rSelected === 3}>Three</Button>
      </ButtonGroup>
      <p>Selected: {rSelected}</p>
    </div>
  );
} 

This action triggers the error message:

Argument of type '1' is not assignable to parameter of type 'SetStateAction<undefined>'. 

How can this issue be resolved?

Answer №1

If you want to utilize useState, make sure to specify the data type as shown below:

const [rSelected, setRSelected] = useState<number | undefined>();

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Need to import Vue component TWICE

My question is simple: why do I need to import the components twice in the code below for it to function properly? In my current restricted environment, I am unable to utilize Webpack, .vue Single File Components, or npm. Despite these limitations, I mana ...

The useState function in React does not update the state

I have a frontend code snippet that deals with authentication: AuthContext.js: const { createContext, useState, useContext } = require("react"); const AuthContext = createContext(); export function useAuth() { return useContext(AuthContext); } export ...

How to open a new tab in ReactJS

When attempting to open a component in a new tab using the Link in React router, it results in a 404 error page being displayed instead of the desired react component. The main React entry point file (js), import React from 'react'; import { re ...

Tips on deleting excess space in between Material-UI Grid Rows

https://i.stack.imgur.com/nASqM.pngWhen attempting to utilize the Material UI grid system, I encountered an odd spacing issue when using grid direction='row'.https://i.stack.imgur.com/sl59V.png const useStyles = makeStyles((theme) = ...

Encountering the "Maximum Update Depth Exceeded" error in React Typescript with hooks

I encountered this error: Uncaught Error: Maximum update depth exceeded. It seems to be related to calling setState multiple times within componentWillUpdate or componentDidUpdate. React limits nested updates to prevent infinite loops. I am unsure of what ...

Unable to cancel the RTK query request

It's quite a dilemma. I need to handle the request differently when there is no user present. I've attempted different approaches like this and that const { data: carts = [] as ICart[], isFetching } = api.useGetCartProductsQuery(user.id, { skip: ...

What steps can I take to ensure my React js Material-UI table is both responsive and evenly spaced?

Currently utilizing the MaterialTable component sourced from material-ui, I've encountered two issues that require resolution. 1. Is there a way to ensure equal spacing of columns? Currently, there seems to be an unnecessary amount of space between t ...

What is the process for building .ts components within a Vue application?

Looking to update an old project that currently uses the 'av-ts' plugin and vue-ts-loader to compile .ts files as Vue components. The project is running on Vue 2.4.2, but I want to upgrade it to version 2.6.14. However, since 'vue-ts-loader& ...

The implementation of Symbol.species in the Node.js Buffer class to generate a RapidBuffer seems illogical and confusing

While exploring the source code of ws, a popular WebSocket implementation for Node.js, I stumbled upon this specific piece of code: const FastBuffer = Buffer[Symbol.species]; But what exactly is this FastBuffer used for? Surprisingly, it seems that they a ...

I'm encountering an issue with my array in JavaScript while using // @ts-check in VS Code. Why am I receiving an error stating that property 'find' does not exist on my array? (typescript 2.7

** update console.log(Array.isArray(primaryNumberFemales)); // true and I export it with: export { primaryNumberFemales, }; ** end update I possess an array (which is indeed a type of object) that is structured in the following manner: const primar ...

Guide to deploying a React Application to Netlify while organizing the client and server directories

I'm facing some confusion while attempting to deploy an application to Netlify that consists of both client and server folders. I initially tried running npm build on the main directory which contains both folders, but it did not yield the desired res ...

React component rendering twice due to width awareness

In a React component that I've developed, I have utilized ResizeObserver to track its own width. Within this component, two divs are rendered with each having a "flex: 1" property to ensure equal width distribution. Under certain conditions, such as w ...

Learn how to utilize react-router for re-rendering components in React with this simple example. Earn +100 reputation

I am new to React, especially with react-router. I came across an example on this website: . I decided to try replicating it on Codesandbox.io as well: https://codesandbox.io/s/7jxq0j6qp0 My issue is that the Menu component keeps re-rendering every time ...

I encountered an issue when attempting to execute an action as I received an error message indicating that the dispatch function was not

I just started learning about redux and I am trying to understand it from the basics. So, I installed an npm package and integrated it into my form. However, when I attempt to dispatch an action, I encounter an error stating that 'dispatch is not defi ...

React encountering difficulty showing images stored on a local path

Just starting out with React and I'm facing an issue while trying to show images from the src/images folder. Even after double-checking the path, the image still doesn't appear on the screen. Take a look at my image tag path and folder structure ...

Storing tasks in the server backend for later access

Struggling to find the most efficient way to store todo list items in the backend. I've heard that storing arrays and objects directly in the backend may not be optimal. Currently working on a web app inspired by Google Keep. Here's some context ...

I am encountering an issue with the react-use-cart package as it requires the string "id" while my API data only provides the object "_id". Is there a way to convert or manipulate the

Seeking assistance with integrating an item into a cart system. The function requires the id of the item, but my item only generates the _id and not simply the id. Grateful for any insights from those with expertise in this area. Thank you in advance! htt ...

Footer displays within the content section

Currently, I am utilizing Next.js and antd's layout components to create a dashboard-style page using their Layout component. However, I have encountered two issues related to styling (CSS) in this setup: Footer appearing within the Content: I am wo ...

Tips for turning off the menu button feature in DataGrid MUI programming

Is there a way to completely disable the button that displays all the options like sort, filter, and hide? https://i.stack.imgur.com/I2xOE.png I am aware that I can remove the sorting option in the columns using sortable: false, as well as use disableCol ...

Zod data structure featuring optional fields

Is there a more efficient way to define a Zod schema with nullable properties without repeating the nullable() method for each property? Currently, I have defined it as shown below: const MyObjectSchema = z .object({ p1: z.string().nullable(), p2 ...