A
A
Anastasia2020-11-27 15:31:09
React
Anastasia, 2020-11-27 15:31:09

How to designate an optional parameter in PropTypes?

I describe a template component that will be used in different parts of the project, its incoming parameters, and describe these parameters through PropTypes. In different parts of the project, the parameters passed to this component will be different, so how can this be reflected in the template? it is necessary to register that some part is optional - using a question mark when describing in PropTypes an error shines. And it is necessary to pass in the template to the component itself all the parameters received by props?

import React from 'react';
import { DatePicker } from 'rsuite';
import { locale } from 'constants/datePickerLocale';
import isEqual from 'deep-equal';
import pt from 'prop-types';

const DateField = ({
  value = null,
  defaultValue = null,
  format,
  changeHandler = undefined,
  block = false,
  size = null,
  placement = null
}) => {
  return (
    <DatePicker
      format={format}
      locale={locale}
      value={value}
      defaultValue={defaultValue}
      onChange={changeHandler}
      block={block}
      size={size}
      placement={placement}
      isoWeek
    />
  );
};

DateField.propTypes = {
  value: pt.instanceOf(Date),
  defaultValue: pt.instanceOf(Date),
  format: pt.string,
  changeHandler: pt.func,
  block: pt.bool,
  size: pt.string,
  placement: pt.string
};

export default React.memo(DateField, isEqual);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Lewandowski, 2020-11-27
@vovaspace

DateField.propTypes = {
  // Необязательные
  propA: pt.string,

  // Обязательные
  propB: pt.string.isRequired,
};

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question