B
B
balex7772021-11-30 11:09:16
JavaScript
balex777, 2021-11-30 11:09:16

How to pass props and children to a component in react typescript?

There is a component

import React from 'react';
import BackButton from 'components/buttons/BackButton';

type PropTypes = {
  title: string,
  goBackText?: string,
  children?: React.ReactElement,
} & typeof defaultProps;

const defaultProps = {
  goBackText: '',
  children: null,
};

const NamePagesBar = ({
  title, goBackText, children,
}:PropTypes) => (
  <div className="mx-auto px-4 h-18 flex items-center flex justify-between">
    <h1>{title}</h1>
    {goBackText && (
      <BackButton text={goBackText} className="flex items-baseline text-sm text-blue hover:text-blue-dark underline " />
    )}
    {children}
  </div>
);

NamePagesBar.defaultProps = defaultProps;

export default NamePagesBar;


I'm trying to send it

<NamePagesBar title={title}>
        <button>efreasrf</button>
      </NamePagesBar>


It gives such errors
61a5db5644291126443068.png
61a5db62c3d07809038463.png

. I think the problem is that children have defaultProps, because in other places I did something similar, but there children was mandatory and there were no problems.
PS I tried to set the ReactNode type for children, but the webstorm gives an error that the button is Element
How to do it right ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexandroppolus, 2021-11-30
@balex777

defaultProps is irrelevant in TS

type PropTypes = {
  title: string
  goBackText?: string
};

const NamePagesBar: React.FC<PropTypes> = ({title, goBackText = ''}) => (<...>);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question