0

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>

2
  • you never use state? Commented Jan 16, 2021 at 7:12
  • @SomeoneSpecial ow to do that I'm beginner to react native. and I'm using class component. and the array is defined out side the class so how i set state to the array data from inside the class and then i can pass it to the app view easily Commented Jan 16, 2021 at 7:16

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

i got this error The 'const' modifier can only be used in TypeScript files. when i used const data = getvals();
function data () { }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.