admin管理员组

文章数量:1327750

I have an SFC React ponent 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.

I have an SFC React ponent 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.

Share Improve this question edited Oct 30, 2018 at 18:44 skyboyer 23.8k7 gold badges62 silver badges71 bronze badges asked Jun 30, 2018 at 11:55 CaribouCodeCaribouCode 14.4k33 gold badges111 silver badges185 bronze badges 2
  • This might be the same issue around setting SFC defaultProps, maybe try looking at this: stackoverflow./questions/40209352/… – K.F Commented Jun 30, 2018 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 Commented Jul 1, 2018 at 8:08
Add a ment  | 

2 Answers 2

Reset to default 5

I'm not sure if you've checkout out: https://github./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 plicated 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;

According to this ment, your Props type can be see as the "internal" types of the ponent. If you want Props to be documentation of the API of the ponent, 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}`} />
};

本文标签: javascriptFlow error for prop string in template literalStack Overflow