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 = {
  childrenReact.ReactNode;
};

export const Counter = (propsButtonProps=> {
  const [countsetCount] = 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 [countsetCount] = useState(0);

  return (
    <>
      <Counter disabled={true} />
    </>
  );
}


Example code 

https://stackblitz.com/edit/vitejs-vite-quhrj5?file=README.md








Comments

Popular posts from this blog

The specified initialization vector (IV) does not match the block size for this algorithm