【问题标题】:Pass data of array to react native view传递数组的数据以反应原生视图
【发布时间】:2021-04-21 03:25:46
【问题描述】:

我有一个从后端获取数据并映射数据然后将它们添加到数组的函数 叫events_data

function getvals() {

    return fetch('http://**********/users/timetable')
        .then((response) => response.json())
        .then((output) => {
            addData(output, events_data);
        })


        .catch(error => console.log(error))

}

function addData(data, data2) {
    data.map((d) => {
        data2.push({
            title: d.name,
            startTime: genTimeBlock(d.day, d.start_time),
            endTime: genTimeBlock(d.day, d.end_time),
            location: d.location,
            extra_descriptions: [d.extra_descriptions],
        });
    });
}

所以在我的应用视图中,我想将 events_data 传递给 events 道具:

 <SafeAreaView style={{ flex: 1, padding: 30 }}>
     <View style={styles.container}>
       <TimeTableView
           scrollViewRef={this.scrollViewRef}
           events={// events.data will be passed here as array format //}**
           pivotTime={8}
           pivotDate={this.pivotDate}
           numberOfDays={this.numOfDays}
           onEventPress={}
           headerStyle={styles.headerStyle}
           formatDateHeader="dddd"
           locale="en"
          />
      </View>
      </SafeAreaView>

旁注:时间表视图是一个第三方包,接受在porpevents={}中传递的数组并以时间表格式显示其数据 所以在这里我想传递来自function addDataevents_data 数组并将其传递给&lt;TimeTableView&gt; 中的events prop

【问题讨论】:

  • 你从不使用状态?
  • @SomeoneSpecial ow 这样做我是初学者反应原生。我正在使用类组件。并且数组是在类之外定义的,所以我如何从类内部将状态设置为数组数据,然后我可以轻松地将它传递给应用程序视图

标签: arrays json react-native fetch-api


【解决方案1】:
function getvals() {

    return fetch('http://**********/users/timetable')
        .then((response) => response.json())
        .then((output) => {
            return addData(output, events_data); //<-- add a return statement.
        })


        .catch(error => console.log(error))

}

在您调用 get Val 函数的类组件中。

const data = getvals();
this.setState({ events: data });

那么你可以在你的表中使用 this.state.events。

【讨论】:

  • 当我使用 const data = getvals(); 时出现此错误 The 'const' modifier can only be used in TypeScript files.
  • 函数数据(){}
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-10-11
  • 2019-03-03
  • 1970-01-01
  • 1970-01-01
  • 2022-07-15
  • 2019-01-22
  • 1970-01-01
相关资源
最近更新 更多