我目前正在开发一个原生react应用程序,但我面临着一个问题。我正在从API中获取数据,数据如下所示
{
"current":{
...
"name": " - Kraftwerk - It's More Fun to Compute ",
...
}
} 为了将It's转换为It's,我想对这个字符串进行解码,以便在我的视图中显示它。
在更新state之前,我尝试使用JavaScript函数decodeURIComponent对其进行解码,但不幸的是它不起作用。我的视图仍然显示It's。
这是我的组件(简化版)
import React, {useState, useEffect} from "react";
import { View, StyleSheet, Text, ActivityIndicator } from "react-native"
const Metadata = props => {
const [track, setTrack] = useState({
name: "",
type: ""
});
const trackinfo = (data) => {
let trackName = decodeURIComponent(data['current']['name']);
let trackType = data['current']['type'];
setTrack({
name: trackName,
type: trackType
});
useEffect(() => {
...
}, []);
return (
<View style={styles.container}>
<Text>
{track['name']}
</Text>
</View>
);
};
export default Metadata;你知道为什么decodeURIComponent()没有效果吗?
谢谢你
发布于 2021-11-21 18:20:42
您可以使用html-entitites库的decode函数。
import {decode} from 'html-entities';
let trackName = decode(data['current']['name']);https://stackoverflow.com/questions/70057329
复制相似问题