react component passing in properties from parent using spread operator
In this example, we are going to pass properties down from parents to a child using the spread operator.
Basically any properties will be pass along. Using the counter example, we will have the following child component code:-
Counter.tsx
import { useState } from 'react';
type ButtonProps = {
children: React.ReactNode;
};
export const Counter = (props: ButtonProps) => {
const [count, setCount] = useState(0);
return (
<>
<button {...props} onClick={() => setCount((count) => count + 1)}>
mycount is {count}
</button>{' '}
</>
);
};
In my parent App.tsx, i have the following codes
import { Counter } from './Counter';
function App() {
const [count, setCount] = useState(0);
return (
<>
<Counter disabled={true} />
</>
);
}
Example code
https://stackblitz.com/edit/vitejs-vite-quhrj5?file=README.md
Comments