简体   繁体   English

React Typescript:类型参数不可分配给类型参数

[英]React Typescript: Argument of type is not assignable to parameter of type

It is a React component to create a Check Box Button, I am having this error in setRSelected(1),setRSelected(2)...它是一个用于创建复选框按钮的 React 组件,我在 setRSelected(1),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>
  );
} 

and I get this error: Argument of type '1' is not assignable to parameter of type 'SetStateAction<undefined>'.我收到此错误: Argument of type '1' is not assignable to parameter of type 'SetStateAction<undefined>'.

How can I fix this我怎样才能解决这个问题

You should declare type when use useState like this:你应该像这样使用useState时声明类型:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React Typescript - 类型参数不可分配给类型参数 - React Typescript - Argument of type is not assignable to parameter of type React TypeScript:参数不可分配给“从不”类型的参数 - React TypeScript: Argument is not assignable to parameter of type 'never' React和Typescript:类型参数不能分配给&#39;EventListenerOrEventListenerObject&#39;类型的参数 - React and Typescript: Argument of type is not assignable to parameter of type 'EventListenerOrEventListenerObject' 与 Typescript 反应:“never[]”类型的参数不可分配给“StateProperties |”类型的参数 (() =&gt; 状态属性)' - React with Typescript: Argument of type 'never[]' is not assignable to parameter of type 'StateProperties | (() => StateProperties)' React Typescript - 'string' 类型的参数不可分配给 useState 中的类型参数 - React Typescript - Argument of type 'string' is not assignable to parameter of type within useState React 组件上的 Typescript 错误:“元素”类型的参数不可分配给类型的参数 - Typescript error on React Component: Argument of type 'Element' is not assignable to parameter of type React Typescript:'{ [x: number]: any; 类型的参数 }' 不可分配给类型的参数 - React Typescript: Argument of type '{ [x: number]: any; }' is not assignable to parameter of type “文件”类型的参数不能分配给“从不”类型的参数。 与 TypeScript 反应 - Argument of type 'File' is not assignable to parameter of type 'never'. React with TypeScript &#39;x&#39;类型的React Typescript参数不可分配给&#39;SetStateAction&#39;类型的参数<x]> &#39; - React Typescript Argument of type 'x' is not assignable to parameter of type 'SetStateAction<x]>' 反应,typescript “数字”类型的参数不可分配给“ChangeEvent”类型的参数<htmlinputelement> '</htmlinputelement> - react, typescript Argument of type 'number' is not assignable to parameter of type 'ChangeEvent<HTMLInputElement>'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM