pass component as props react typescript code example

Example 1: react typescript props

// Declare the type of the props
type CarProps = {
  name: string;
  brand: string;
  price;
}

// usage 1
const Car: React.FC<CarProps> = (props) => {
  const { name, brand, price } = props;
  // some logic
}

// usage 2
const Car: React.FC<CarProps> = ({ name, brand, price }) => {
	// some logic
}

Example 2: react typescript pass component as prop

interface ParentCompProps {
  childComp?: React.ReactNode;
}

const ChildComp: React.FC = () => <h2>This is a child component</h2>

const ParentComp: React.FC<ParentCompProps> = (props) => {
  const { childComp } = props;
  return <div>{childComp}</div>;
};

function App() {
  return (
    <>
      <ParentComp childComp={<ChildComp />} />
      <ParentComp childComp={<h3>Child component 2</h3>} />
      <ParentComp childComp={(
        <div style={{border: '2px solid red'}}>
          <h4>Child component</h4>
          <p>With multiple children</p>
        </div>
      )} />
    </>
  );
}

Example 3: passing props using ts

playOrPause : 'Play'