javascript - React, passing state setter (useState) to child component with TypeScript - Stack Overflow

I'm having a hard time figuring out the types when passing the useState setter function to a child

I'm having a hard time figuring out the types when passing the useState setter function to a child ponent.

I've tried to simplify to just the essential code below:

parent

function Parent() {    
  const [name, setName] = useState("Structured")
  ..
  return (
    <>
      <Child setName={setName}/>
    </>

child

import { Dispatch, SetStateAction } from "react";

function Child(setName: Dispatch<SetStateAction<string>>){
  return (
    <>
      <Input onChange={
        (value)=>{
          setName(value: SetStateAction<string>)
          console.log(value)
        }
      </Input>
    </>

On the parent I'm getting the following errors:

Type "{ setName: Dispatch<SetStateAction>; }' is not assignable to type ' IntrinsicAttributes & Dispatch<SetStateAction' Property 'setName' does not exist on type 'IntrinsicAttributes & Dispatch<SetStateAction>'.

On the child I'm getting:

Argument of type "string | string[]' is not assignable to parameter of type 'SetStateAction' Type "string[]' is not assignable to type "SetStateAction'

I'm having a hard time figuring out the types when passing the useState setter function to a child ponent.

I've tried to simplify to just the essential code below:

parent

function Parent() {    
  const [name, setName] = useState("Structured")
  ..
  return (
    <>
      <Child setName={setName}/>
    </>

child

import { Dispatch, SetStateAction } from "react";

function Child(setName: Dispatch<SetStateAction<string>>){
  return (
    <>
      <Input onChange={
        (value)=>{
          setName(value: SetStateAction<string>)
          console.log(value)
        }
      </Input>
    </>

On the parent I'm getting the following errors:

Type "{ setName: Dispatch<SetStateAction>; }' is not assignable to type ' IntrinsicAttributes & Dispatch<SetStateAction' Property 'setName' does not exist on type 'IntrinsicAttributes & Dispatch<SetStateAction>'.

On the child I'm getting:

Argument of type "string | string[]' is not assignable to parameter of type 'SetStateAction' Type "string[]' is not assignable to type "SetStateAction'

Share Improve this question edited May 25, 2022 at 20:11 Youssouf Oumar 46.6k16 gold badges103 silver badges105 bronze badges asked May 25, 2022 at 19:50 AshburyAshbury 2,3563 gold badges32 silver badges63 bronze badges 1
  • 1 Child(setName: Dispatch<SetStateAction<string>>) shoud be Child({setName: Dispatch<SetStateAction<string>>}) – pta Commented May 25, 2022 at 20:28
Add a ment  | 

2 Answers 2

Reset to default 5

Should be

function Child({ setName } : { setName: Dispatch<SetStateAction<string>>}){

Child ponent accepts props, which is an object, not just setName value

You could do it with the help of FC from React, if you want for example to be explicit about the return as well:

import { Dispatch, SetStateAction, FC} from "react";

interface ChildPropsType {
  setName: Dispatch<SetStateAction<string>>
}
const Child : FC<ChildPropsType> = ({setName}) => {
  return (
    <>
      <Input onChange={
        (value as string)=>{
          setName(value);
        }
      </Input>
    </>
  )
}
export default Child;

For the onChange part I'm a doing a type cast as I don't know how this Input looks like.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744596646a4582930.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信