Pass data of array to react native view

百般思念 提交于 2021-01-29 05:21:05

问题


I have a function that fetch the data from backend and map over the data and then add them to array called 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],
        });
    });
}

So in my app view I want to pass events_data to events props:

 <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>

Side note: the timetable view it is a third party package that accept array passed in porp events={} and display its data in timetable format so here I want to pass events_data array coming from function addData and pass it to events prop in <TimeTableView>


回答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))

}

in your class component where you are calling the get Val function.

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

then you can use this.state.events in your table.



来源:https://stackoverflow.com/questions/65747282/pass-data-of-array-to-react-native-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!