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.
- 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
2 Answers
Reset to default 5I'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}`} />
};