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.
- 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}`} />
};
本文标签: javascriptFlow error for prop string in template literalStack Overflow
版权声明:本文标题:javascript - Flow error for prop string in template literal - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742230774a2437182.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论