导入 'dayjs/local/en' 时出现错误?

Getting error when I import 'dayjs/local/en'?

我想为我的 react-native-gifted-chat 添加语言环境,因为聊天不会一个接一个地显示,我猜是因为时区的原因。

我在我的天才聊天中添加了语言环境:

 import en from 'dayjs/locale/en'

 render() { 
    return (
        <View style={{backgroundColor: '#000000', flex: 1}}>
            <GiftedChat
                showUserAvatar={true}
                isTyping={this.state.isTyping}
                renderUsernameOnMessage={true}
                messages={this.state.messages}
                onSend={message => this.onSend(message)}
                scrollToBottom
                locale = { en } <-----------------------------
                showAvatarForEveryMessage = {false}
                showUserAvatar= {true}
                dateFormat = 'll'
                timeFormat = 'LT'
                placeholder = "Talk to people..."
                onPressAvatar={user => this.getProfile(user)}
            />
        </View>
        
    )

但现在我得到了错误:

TypeError: undefined is not an object (evaluating 'n[M].replace')

这是因为我使用了错误的导入类型,还是现有的聊天记录有问题,我是否需要删除它们才能正常工作?

查看文档,您似乎需要单独导入语言环境文件。

首先你需要导入daysjs:

import dayjs from 'dayjs';

然后你需要导入你想要的语言环境:

import 'dayjs/locale/en'

这似乎具有使该语言环境可用于格式化的全局突变副作用(坏)。

在你的prop中,传入locale="en"

然后在你的组件中(如果你拥有它)你可以使用 dayjs(date).locale(this.props.locale).format()

Daysjs 看起来不像是一个设计良好的库,因为导入不应该有这样的副作用。