I basically have a button element pretty far down the component hierarchy so I'm passing a function from my App level downwards so that it can be called onClick within the button. I feel like I've correctly defined both the App function (newTodoCard()) as :()=>void and the prop of child component (onAddTodo()) as :()=>void. I would prefer to avoid defining an entire Interface for one function prop and want to understand why my approach isn't working.
App.tsx
import React from 'react';
import Header from './components/Header';
import Sidebar from './components/Sidebar';
import TodoCard from './components/TodoCard';
import { TodoCardProps } from './components/TodoCard';
import { useState } from 'react';
function App() {
const[todoCards,setTodoCards] = useState([]);
let currentTodoCard: TodoCardProps = {title:"",content:""};
const newTodoCard: ()=>void = ()=>{
currentTodoCard = {title:"NEW",content:"NEW"};
}
return (
<div className="App">
<Header/>
<div className="container">
<Sidebar {...newTodoCard}/>
<TodoCard {...currentTodoCard}/>
</div>
</div>
);
}
export default App;
This snippet from above is where the error is:
<Sidebar {...newTodoCard}/>
Sidebar.tsx
import React from 'react'
import TitleCards from './TitleCards'
import SidebarHeader from './SidebarHeader'
const Sidebar = ( onAddTodo: ()=>void) => {
return (
<div className="sidebar">
<SidebarHeader {...onAddTodo}/>
<TitleCards/>
</div>
)
}
export default Sidebar
Additionally, if I change how I pass in the prop to
<Sidebar onAddTodo={newTodoCard}/>
It seems to solve the issue, but this error Type '{ onAddTodo: () => void; }' is not assignable to type 'IntrinsicAttributes & (() => void)'. Property 'onAddTodo' does not exist on type 'IntrinsicAttributes & (() => void)' appears (which from online research is only fixed by using {...prop} as I did originally. I appreciate any help! Thanks.