【问题标题】:I can't return parsing information in React Native我无法在 React Native 中返回解析信息
【发布时间】:2020-04-10 17:22:18
【问题描述】:

我是一名初学者程序员,我决定制作我的第一个项目 - 天气应用程序。虽然我有问题,但我无法将解析的信息显示在屏幕上,但如果我使用 console.log,信息会显示在控制台中。

我在现场做所有这些:https://snack.expo.io/

                   Copying you the codes and comment inside it console.logs


        **CODE**

 import React, { Component, createElement } from 'react';
    import {
      StyleSheet,
      Text,
      View,
      Image,
      ImageBackground,
      ActivityIndicator,
    } from 'react-native';

    class App extends Component {
      constructor(props) {
        super(props);
        this.state = {
          todos: [],
          isLoading: true,
        };
      }

      componentDidMount = async () => {
        fetch(
          'API'
        )
          .then(res => res.json())
          .then(data => {
            this.setState({ todos: data });

          })
          .catch(error => console.error(error))
          .finally(() => {
            this.setState({ isLoading: false });
          });
      };

      render() {
        const { data, isLoading } = this.state;
        return (
          <View>
          <Text>
          {data.sys.id}
          </Text>
          </View>
        );
      }
    }

    export default App;

谢谢!

【问题讨论】:

    标签: json react-native parsing weather-api


    【解决方案1】:

    我通过创建一个与您的 api 类似的天气 api 对您的代码进行了处理,并对现在工作的代码进行了一些更改,请检查此代码。

    这是我从 API 获取的天气数组,我也使用天气 API 作为你的

     Object {
        "description": "light intensity drizzle",
        "icon": "09d",
        "id": 300,
        "main": "Drizzle",
      },
    

    代码:

    import React, { Component, createElement } from 'react';
            import {
              StyleSheet,
              Text,
              View,
              Image,
              ImageBackground,
              ActivityIndicator,
            } from 'react-native';
    
            class App extends Component {
              constructor(props) {
                super(props);
                this.state = {
                  todos: [],
                  isLoading: true,
                };
              }
    
              componentDidMount = async () => {
                fetch(
                  'https://samples.openweathermap.org/data/2.5/weather?q=London&appid=439d4b804bc8187953eb36d2a8c26a02'
                )
                  .then(res => res.json())
                  .then(data => {
                    this.setState({ todos: data });
                  })
                  .catch(error => console.error(error))
                  .finally(() => {
                    this.setState({ isLoading: false });
                  });
              };
    
              render() {
                return (
                    <View style={{flex:1,justifyContent:"center",alignItems:"center"}}>
                        <Text>Hello</Text>
                        <Text>
                            {this.state.todos.weather != undefined ? this.state.todos.weather[0].description : null}
                        </Text>
                        <Text>
                            {this.state.todos.weather != undefined ? this.state.todos.weather[0].main : null}
                        </Text>
                    </View>
                );
              }
        }
    
        export default App;
    

    希望这会有所帮助!

    【讨论】:

    • 非常感谢您的回答。但是,当我改变这一切时,我有同样的错误: - undefined is not an object (evalating 'this.state.todos.weather[0]') =( @
    • 对我的代码进行了一些更改,您能否通过在 setState 之后打印待办事项来检查您在控制台中得到了什么
    • 感谢您,很抱歉打扰您。我尝试了很多不同的更改,我也尝试了您的版本,但每次问题都是相同的 - “未定义不是对象”。另外,当我在 setState 之后编写 console.log(this.state.todos) 时,它会写入所有数组和对象 @..
    • 这个错误显示的原因是渲染函数在获取api之前调用你可以做的是将渲染函数更改为 {this.state.todos !== [] ? this.state.todos[0].description : null} 检查一下我认为它会起作用,对我的答案进行了更改检查一次。
    • 感谢您的耐心等待。它仍然不起作用。当我使用返回 {this.state.todos} 并使用 this.setState({todos:data.weather}) 我有错误:“对象作为 React 子项无效(找到:对象带有键 {id, main, description, icon})" 。但是,当我将 setState 更改为 this.setState({todos:data}) 并尝试返回时,我遇到了这个错误-“对象作为 React 子项无效(找到:带有键的对象 {coord, weather, base, main , 能见度, 风, 云, dt, sys, timezone, id, name, cod})”。 @
    【解决方案2】:

    首先,您使用了错误的对象名称。

    这是错误的。

    const { data, isLoading } = this.state;
    

    这是对的。

    const { todos, isLoading } = this.state;
    

    其次 这是因为您试图访问未定义的对象。

    应用程序将加载并呈现页面。然后发出 Api 请求。 所以数据不存在。您可以设置默认值或添加检查。

    render() {
        const { todos, isLoading } = this.state;
          return (
            <View>
              <Text>
               {todos.weather > 0 && todos.weather[0].description}
              </Text>
            </View>
          );
       }
    

    【讨论】:

    • 非常感谢您的回答。但是,它仍然不起作用:(我尝试了很多wariants,但没有任何结果(我有两个版本的错误:1)对象未定义或2)对象作为React子对象无效(发现:带键的对象{id, main, description, icon}) @<13261398>
    猜你喜欢
    • 2021-05-04
    • 2018-09-16
    • 1970-01-01
    • 2022-11-17
    • 2020-01-18
    • 1970-01-01
    • 2016-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多