12

I am learning typescript with react and I occurred problem. I tried to pass function as a prop from my App component to child component named DataForm.

but I got an error:

Type '(f: any) => any' is not assignable to type '() => void'.
Parameter 'f' implicitly has an 'any' type.

And this is my code

App.tsx

import React from 'react';
import './App.css';
import DataForm from './components/form';

export const Data = {
  name: "",
  country: "",
  age:"",
  adress:""
}

function App() {


  const generateCard = ()=>{
    console.log(" generateCard runned")
  }
  return (
    <>
      <h1>Human Card Generator</h1>
      <DataForm  createCard = { generateCard }/>
    </>
    

  );
}

export default App;

components/form.tsx

import React from 'react'
import { Data } from '../App'
import 'bootstrap';

interface dataFormProps{
    createCard: ()=>void
}

export default function DataForm({ createCard = f => f }: dataFormProps){

    const dataProceed = (e: any) =>{
        Data.name = (document.getElementById('name') as HTMLInputElement).value;
        Data.country = (document.getElementById('country') as HTMLInputElement).value;
        Data.age = (document.getElementById('age') as HTMLInputElement).value;
        Data.adress = (document.getElementById('adress') as HTMLInputElement).value;
        createCard();
    }


    return(
        <form onSubmit = { dataProceed }>
            <input type="text"   id = "name"  />
            <input type="text"  id = "country" />
            <input type="number" id="age" />
            <input type="text" id = "adress"/>
            <button type="submit">stwórz kartę</button>
        </form>
    )
}

2
  • dataFormProps explicitly doesn't match your default function--you have to decide on a function signature, regardless of what it is, and make both explicitly-passed and default functions match that signature. Commented Aug 23, 2021 at 15:23
  • Well you defined createCard as ()=>void but then you default it to f => f. So the default value for createCard does not match () => void Commented Aug 23, 2021 at 15:26

2 Answers 2

16

The issue isn't when passing the function, it's when destructuring the props and providing a default function:

{ createCard = f => f }: dataFormProps

This code indicates that createCard should be a function which accepts a parameter and returns a value. Which it doesn't:

createCard: ()=>void

Make the default parameter match the signature:

{ createCard = () => {} }: dataFormProps
Sign up to request clarification or add additional context in comments.

Comments

6

default function createCard should has type () => void. So just update like this:

DataForm({ createCard = () => {} }: dataFormProps)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.