javascript - 如何在 React-Native collapsible/accordion 中使用外部源数据

标签 javascript reactjs react-native

我在我的项目中使用react-native collapsible/accordion。我找到了一个例子,如下

import React, {` Component } from 'react-native';
import Accordion from 'react-native-collapsible/Accordion';

const SECTIONS = [
  {
    title: 'First',
    content: 'Lorem ipsum...',
  },
  {
    title: 'Second',
    content: 'Lorem ipsum...',
  }
];

class AccordionView extends Component {
  _renderHeader(section) {
    return (
      <View style={styles.header}>
        <Text style={styles.headerText}>{section.title}</Text>
      </View>
    );
  }

  _renderContent(section) {
    return (
      <View style={styles.content}>
        <Text>{section.content}</Text>
      </View>
    );
  }

  render() {
    return (
      <Accordion
        sections={SECTIONS}
        renderHeader={this._renderHeader}
        renderContent={this._renderContent}
      />
    );
  }
}

我已将数据存储在这样的状态

state = { orders: [] };

  componentWillMount() {
    axios.get('https://jsonblob.com/api/jsonBlob/c6ccf4e2-3871-11e7-ae4c-bb26ea80a2f8')
      .then(response => this.setState({ orders: response.data }));
  }

所以我的问题是如何在上面的示例代码中使用我的数据(orders)而不是SECTIONS。谢谢您

最佳答案

开始吧,只需将 SECTIONS 替换为 this.state.orders

    render() {
      return (
        <Accordion
          sections={this.state.orders}
          renderHeader={this._renderHeader}
          renderContent={this._renderContent}
        />
      );
    }

关于javascript - 如何在 React-Native collapsible/accordion 中使用外部源数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44012923/

相关文章:

javascript - 何时使用 HOC。何时使用 Function 作为子函数

javascript - 为什么我在运行此示例 node.js 代码时得到 "TypeError: object is not a function"?

amazon-web-services - 在AWS S3中部署react-redux应用程序

javascript - 是否有与 <noscript> 相反的 HTML?

javascript - react 形式 : Updating the UI based on the Axios GET call response in withFormik()

javascript - 如何在浏览器控制台或外部脚本中调用 Reactjs 组件的方法?

react-native - native react - 具有百分比值的 paddingTop 不起作用

java - Android 项目失败 (React Native) - 任务 ':app:packageDebug' 执行失败 APK META-INF/LICENSE 中复制的重复文件

react-native - 在React Native中与Android OS设置链接

javascript - 单击休息按钮时如何休息动态文本框值