首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将JSON feed (WP feed)解析为React Native feed页面

将JSON feed (WP feed)解析为React Native feed页面
EN

Stack Overflow用户
提问于 2017-07-10 23:32:46
回答 1查看 149关注 0票数 0

当我解析JSON提要(Wordpress JSON提要)到我的React Native提要页面时,我在模拟器中没有看到任何结果,以下是我使用的代码;

-AppBody.js

代码语言:javascript
复制
    import React, {Component} from 'react';
    import {Text, View, Image, StyleSheet, Dimensions} from 'react-native';
    import {Content, Container, Card, CardItem, Body} from 'native-base';

    import AppBodyData from './AppBodyData';

    export default class AppBody extends Component {

      constructor() {
        super()
        this.state={
          data:[]
        }
      }

      getData() {

        return fetch('https://www.rayaccountants.co.uk/feed/json')
              .then((response) => response.json())
              .then((responseJson) => {
                this.setState({data: responseJson.items});
              })
              .catch((error) => {
                console.error(error);
              });
      }

      componenetDidMount(){
        this.getData();
      }

      render() {
        return (
          <Container style={{
            marginTop: 100
            }}>
          <AppBodyData data={this.state.data}/>
          </Container>
        );
      }
    }

    module.export = AppBody;

-AppBodyData.js

代码语言:javascript
复制
    import React, {Component} from 'react';
    import {Text, View, Image, StyleSheet, Dimensions} from 'react-native';
    import {Content, Container, Card, CardItem, Body} from 'native-base';

    export default class AppBodyData extends Component {
      render() {

        let articles = this.props.data.map(function(articlesData, index) {
          return (
            <Card>
              <CardItem>
                <Body>
                  <Text>
                    {articlesData.title}
                  </Text>
                </Body>
              </CardItem>
            </Card>
          )
        });

        return (
          <Container style={{
            marginTop: 60
          }}>
            <Container>
              {articles}
            </Container>
          </Container>
        );
      }
    }

    module.export = AppBodyData;

下面是我在模拟器上得到的东西,不知道我可能忽略了什么?

iOS Simulator Screenshot

EN

回答 1

Stack Overflow用户

发布于 2017-07-11 00:14:00

将此this.setState({data: responseJson.items})更改为this.setState({data: responseJson.items} ,() => console.log(this.state.data)),您应该能够看到是否正确获取了数据。如果它确实记录了,那么这只是一个样式问题,你可能只需要添加高度/宽度就可以在屏幕上看到它们的渲染。而且,当你已经在做module.export的时候,你也不需要做export default

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45016105

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档