【问题标题】:Getting the data in console but not able to see any api call data on the screen React Native在控制台中获取数据但无法在屏幕上看到任何 api 调用数据 React Native
【发布时间】:2020-05-29 20:26:27
【问题描述】:

嗨,我是新来的本地人。我正在尝试调用任何 api,但无法在应用程序屏幕上看到任何数据,但可以在控制台上看到数据。我认为这个问题与我的状态有关,但是每当我尝试在状态上添加任何内容时,它都会给我一个响应“json.data'不是对象。请指导我。提前致谢。

export default App = () => {
    const [isLoading, setLoading] = useState(true);

    const [data, setData] = useState([]);

    useEffect(() => {
      fetch('https://URL', {
        method: 'GET',
        timeout: 10000,
        headers: {
            Accept: 'application/json',
            'Content-Type': 'application/json'
        },
         body: JSON.stringify({
            "this": "",
            "this 2": ""
        }) 
      })
      .then((response) => response.text())
      .then((responseData) => { console.log(responseData); })
      .then((json)=> setData(text(json.data)))
      .catch((error) => console.error(error))
      .finally(() => setLoading(false)); [];
    }, []);

    return (
        <ScrollView style={styles.container}>
            {isLoading ? <ActivityIndicator /> : (

               <Text> {data.text} </Text>

               <FlatList
                 data={data}   
                 renderItem={({ item }) => (
                   <Text>{item.skill}</Text>
                 )}
               />
            )}
        </ScrollView>
    );
};

【问题讨论】:

    标签: api react-native


    【解决方案1】:

    您必须在显示之前解析 JSON:

    编辑:我将 response.text() 替换为 response.json() 以正确处理 JSON(例如:解析)。

    export default App = () => {
        const [isLoading, setLoading] = useState(true);
    
        const [data, setData] = useState([]);
    
        useEffect(() => {
          fetch('https://URL', {
            method: 'GET',
            timeout: 10000,
            headers: {
                Accept: 'application/json',
                'Content-Type': 'application/json'
            },
             body: JSON.stringify({
                "this": "",
                "this 2": ""
            }) 
          })
          .then((response) => response.json())
          .then((data) => setData(json.data))
          .catch((error) => console.error(error))
          .finally(() => setLoading(false)); [];
        }, []);
    
        return (
            <ScrollView style={styles.container}>
                {isLoading ? <ActivityIndicator /> : (
                   <FlatList
                     data={data}   
                     renderItem={({ item }) => (
                       <Text>{item.skill}</Text>
                     )}
                   />
                )}
            </ScrollView>
        );
    };
    

    【讨论】:

    • 感谢您的帮助。我试图解析数据,但它在控制台中向我显示承诺错误“未处理的承诺拒绝:SyntaxError: JSON Parse error: Unexpected EOF] * [native code]:null in parse”。
    • .then((response) => response.json()) .then((json)=> setData(json.data)) .catch((error) => console.error(error )) .finally(() => setLoading(false)); []; }, []); index} renderItem={({ item }) => ( {item.skill} ) } //
    • @LuvYHWH 我做了一些修复。可以试试吗?
    • @LuvYHWH 很好用!请将答案标记为已解决。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-16
    • 2021-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 2018-07-02
    相关资源
    最近更新 更多