4

I have an SFC React component with Flow running like so:

// @flow

import React from 'react';

type Props = {
  placeholderText?: string,
};

const defaultProps = {
  placeholderText: '',
};

const Textarea = (props: Props) => (
  <textarea placeholder={`${props.placeholderText}`} />
);

Textarea.defaultProps = defaultProps;
export default Textarea;

I get the following error from Flow:

Cannot coerce 'props.placeholderText' to string because undefined[1] should not be coerced (References: [1])

Can someone explain what this is about and what the fix would be?

As far as I can tell, I have explicitly told Flow that the placeholderText IS a string, and furthermore, since it's not a required prop, I've set a default prop as an empty string, so it's never null or undefined.

skyboyer
  • 22,209
  • 7
  • 57
  • 64
CaribouCode
  • 13,998
  • 28
  • 102
  • 174
  • This might be the same issue around setting SFC defaultProps, maybe try looking at this: https://stackoverflow.com/questions/40209352/how-to-specify-optional-default-props-with-typescript-for-stateless-functiona/40209674 – K.F Jun 30 '18 at 12:27
  • @K.F Thanks for your help but I've not made any progress along that thread partly because it's to do with Typescript but also a couple of things I tried from it didn't help. – CaribouCode Jul 01 '18 at 08:08

2 Answers2

4

I'm not sure if you've checkout out: https://github.com/facebook/flow/issues/1660

It seems like a number of people are talking about this issue. Unfortunately I don't really think any of the suggested methods are especially grand.

The first is SFC specific, you could do something like this instead.

const Textarea = ({placeholderText = ""}: Props) => (
  <textarea placeholder={`${placeholderText}`} />
);

^ here we set a default as we destructure placeholderText from props. It would work for your example, but for other more complicated cases it would not be ideal.

The other option isn't ideal either: to remove the optional type from placeholderText to effectively hack around the error:

import React from 'react';

type Props = {
  placeholderText: string,  // here's the change
};

const defaultProps = {
  placeholderText: '',
};

const Textarea = (props: Props) => (
  <textarea placeholder={`${props.placeholderText}`} />
);

Textarea.defaultProps = defaultProps;
export default Textarea;
K.F
  • 459
  • 3
  • 12
  • Thanks for your the suggestions. It's a real eye opener that Flow can't handle this simple thing correctly. Makes me wonder if Typescript would do it better. – CaribouCode Jul 02 '18 at 08:03
  • Your first option, although not ideal works by setting `({ placeholderText = defaultProps.placeholderText }: Props)` – CaribouCode Jul 05 '18 at 09:41
2

According to this comment, your Props type can be see as the "internal" types of the component. If you want Props to be documentation of the API of the component, you could use a default value in the function instead:

type Props = {
  placeholderText?: string,
};

const Textarea = (props: Props) => {
  const { placeholderText = '' } = props;
  return <textarea placeholder={`${placeholderText}`} />
};
Tholle
  • 108,070
  • 19
  • 198
  • 189
  • Thanks for your help, but this technique doesn't pass Airbnb eslint rules (prop isn't required but has no corresponding defaultProp) and isn't the way React docs specify how to apply default values. – CaribouCode Jul 01 '18 at 07:57
  • @Coop That's unfortunate. You're right that `defaultProps` is a better way of setting default props, but there seem to be no way to have both optional props in the types and `defaultProps` in Flow at this time. – Tholle Jul 01 '18 at 09:04
  • 1
    Thanks for your help. I'm quite shocked Flow can't handle this, it seems like such a basic requirement. – CaribouCode Jul 02 '18 at 08:01