gpt4 book ai didi

typescript - 使用 React Intl 和 Typescript 检查消息是否为空

转载 作者:行者123 更新时间:2023-12-04 16:09:33 25 4
gpt4 key购买 nike

我有一个使用 React Intl 和 TypeScript 的应用。

我想显示一个带有一些信息文本的框,并且我希望它仅当文本实际存在时才显示。

可是我做不到

<FormattedMessage id="my.id" />

因为如果 my.id 没有值,React Intl 将回退到消息 ID,即 my.id

所以我想做的是

const myId: string = 'myId';
const info: string = <FormattedMessage id={myId} />;
const infoExists: boolean = myId === info;

但是,infoJSX.Element,而不是string

有什么办法可以做这样的事情吗?

最佳答案

我想通了。

const id: string = 'id';
const componentToBeRenderedIfIdHasValue = ...;

<FormattedMessage id={id}>
{
(message: string) =>
message === id
? null
: componentToBeRenderedIfIdHasValue
}
</FormattedMessage>

希望对大家有所帮助

关于typescript - 使用 React Intl 和 Typescript 检查消息是否为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45209991/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com