react-currency-format error render error text string must be rendered within a < Text>component in Android

mznpcxlj  于 2023-10-22  发布在  React
关注(0)|答案(2)|浏览(182)

我刚刚react-native项目,我使用react-currency-format在我的代码中以正确的货币格式总价。代码在web上运行正常,但在android和ios上有错误**“文本字符串必须在组件内呈现”**。那我怎么才能修好它呢?
这是我的代码

import { View, Text, TouchableOpacity } from 'react-native'
import React from 'react'
import { useSelector } from 'react-redux'
import { selectBasketItems, selectBasketTotal } from '../features/basketSlice'
import { useNavigation } from '@react-navigation/native'
import NumberFormat from 'react-currency-format';

const BasketIcon = () => {
    const items = useSelector(selectBasketItems);
    const navigation = useNavigation();
    const basketTotal = useSelector(selectBasketTotal);          

const total = parseFloat(basketTotal);

  return (
    <View className="absolute bottom-10 w-full z-50">
      <TouchableOpacity className="bg-[#00CCBB] mx-5 p-4 round-lg flex-row items-center space-x-1">
        <Text className=" text-white font-extrabold text-lg bg-[#01A296] py-1 px-2 ">{items.length}</Text>
        <Text className="flex-1 text-white font-extrabold text-lg text-center">View Basket</Text>
        <Text className="text-lg text-white font-extrabold">
          {basketTotal && (
            <NumberFormat
              value={basketTotal}
              displayType={'text'}
              thousandSeparator={true}
              prefix={'₫'} 
            />
          )}
        </Text>

      </TouchableOpacity>
    </View>
  )
}

export default BasketIcon
bq3bfh9z

bq3bfh9z1#

我假设这个库不是供RN使用的,通过将“DisplayType”设置为文本,库将返回导致此错误的元素中的值。这是库中的源代码

if (displayType === 'text') {
      return renderText ? (renderText(value) || null) : <span {...otherProps}>{value}</span>;
    }

你可以试试这个库react-native-format-currency

waxmsbnn

waxmsbnn2#

如果你想在react native中使用这个包,你应该像下面这样设置renderText:

<NumberFormat
          value={basketTotal}
          displayType={'text'}
          thousandSeparator={true}
          prefix={'₫'} 
          renderText={formattedValue => <Text>{formattedValue}</Text>}
        />

相关问题