Home > Mobile >  Array type is not assignable to parameter of type MouseEvent<HTMLElement, MouseEvent>
Array type is not assignable to parameter of type MouseEvent<HTMLElement, MouseEvent>

Time:10-21

Passed state from Parent to child and managed state in the child component. Worked if I set any but not with Iform

Error details: Argument of type 'IRemark[] | undefined' is not assignable to parameter of type 'MouseEvent<HTMLElement, MouseEvent>'. Type 'undefined' is not assignable to type 'MouseEvent<HTMLElement, MouseEvent>'.ts(2345)

Parent.tsx

export IDetails {
   id?: string
    text: string
}

export interface IMasterForm {
   details?: IDetails[]
}

const InitialState: IMasterForm = {
    details: [
        {id: "1234567879", text:"hello world"}
    ]
} 

const Page = () => {
const [details, setDetails] = useState(InitialState.details)

 const wrapper= useCallback(
    (val: any) => {
        setsDetails(val);
    },
    [setDetails]
  );

retrun(
  <Details_Form wrapper={wrapper} details={details}/>
)

this is diffrent project which will be integrated to the parent project

Details_Form.tsx

export IChildDetails {
   id?: string
    text: string
}

export interface IChildForm  {
    details?: IChildDetails []
    wrapper: React.MouseEventHandler<HTMLElement>
}
 //const compOne = ({ wrapper, details} : any) => { //working
 const compOne = ({ wrapper, details} : IChildForm ) => {

  const [childState, setChildState] = useState(details);

  useEffect(() => {
    wrapper(childState);  //<----- error type undefined not assignable to mosue event html ele 
  }, [wrapper, childState]);

CodePudding user response:

You have different types for the function in the parent and the child.

In the parent, the type is (val: any) => void and in the child it's React.MouseEventHandler<HTMLElement>. Obviously, the MouseEventHandler is going to expect a MouseEvent as the argument. And you are passing childState to it, which id of type IChildDetails[]. You should change wrapper: React.MouseEventHandler<HTMLElement> to wrapper: (val: any) => void in the IChildForm interface. wrapper: (val: IChildForm[]) => void should also work.

  • Related