I am pretty new to typescript. trying to build a simple crud app with react, typescript and hooks. I can't figure out how to pass data and function together as props to a child component that will further send props to his chid. Here is a sample code.
parent component
const App = () => {
const [data, setData] = useState([
{ id: 1, name: 'John', email: '[email protected]' },
{ id: 1, name: 'Mike', email: '[email protected]' },
])
// how to send this function to StudentList component
const removeStudent = (id: number): void => {
setData(data.filter(item => item.id !== id))
}
return (
<div>
<StudentList data={data} removeStudent={removeStudent} />
</div>
);
}
child component / StudentList
interface StudentsInterface {
id: number,
name: string,
email: string,
}
interface PropsFunction {
removeStudent: () => void
}
const StudentList = ({ data }: { data: StudentsInterface[] }, { removeStudent }: PropsFunction) => {
console.log('removeStudent ==>', removeStudent) // getting undefined
return (
<table id='students'>
<tbody>
{data.map((student, index) => {
return (
<Student student={student} key={index} removeStudent={removeStudent} />
)
})}
</tbody>
</table>
)
}
If I just pass data (first parameter of StudentList), I get the props, but I want removeStudent function too...
If it is just react I know I would just destructure {removeStudent} in studentList and I am done, but here in typescript I have to define data type...
Hope I am clear. Since I am pretty new to typescript I would be glad if you explain what I am doing wrong.
const StudentList = ({ data, removeStudent } =>, and I suspect the typescript version expectrs something similarconst StudentList = ({ data: StudentsInterface[], removeStudent: PropsFunction } =>