简体   繁体   中英

React Native Wix Calendar Marked Dates showing error

I am using wix calendar for my project, I have already followed all steps mentioned in the other question with similar topic.

I have coded like this

 class TutorCalender extends Component { static navigationOptions = { header: null, final_array: {}, tution_array: [], unavailable_array: [] } componentWillMount = async () => { try { let { data } = await axios .post("https://chat.qualpros.com/api/get_tutor_calendar", { tutor_id: 4 }) .then(response => { console.log(response.data.data.tutor_private_tution_date_array); if (response.data.data.status === "success") { this.setState({ tution_array: response.data.data.tutor_private_tution_date_array, unavailable_array: response.data.data.tutor_schedule_unavailable_array, }); } else { alert("Something went wrong"); } }); } catch (err) { console.log(err); } // console.log(this.state.tution_array.length); //console.log(this.state.tution_array); if (this.state.tution_array.length !== 0) { console.log('tution available') var objP = Object.assign( ...this.state.tution_array.map(o => ({ [o]: { selected: true, selectedColor: "green", type: "present" } })) ); this.setState({ final_array : objP}); console.log(this.state.final_array) } // var merged = { ...objP }; // // console.log("Came Here"); // this.setState({ final_array: merged }); // console.log(this.state.final_array) } render() { return ( <Container> <Header style={{ backgroundColor: '#d91009' }}> <Left style={styles.left}> <TouchableOpacity style={styles.backArrow} onPress={() => this.props.navigation.navigate("ProfileScreen")} > <FontAwesome name="angle-left" size={30} color='#fff' /> </TouchableOpacity> </Left> <Body> <Text style={{ alignSelf: Platform.OS == 'android' ? 'center' : 'center' }}>Tutor calender</Text> </Body> <Right /> </Header> {/* <Text style={styles.text}>Calendar with marked dates and hidden arrows</Text> */} <Calendar style={styles.calendar} firstDay={1} //markedDates={this.state.final_array} //hideArrows={true} /> </Container> ); } } export default TutorCalender; 

if I comment markedDates prop it is working fine and for console.log(this.state.final_array) I am getting the response like this : Object { "2018-10-01": Object { "selected": true, "selectedColor": "green", "type": "present", }, }

but when I uncomment markedDates={this.state.final_array} it is giving me error "TypeError: null is not an object(evaluating 'this.state.final_array'"

I don't understand where I am wrong, I have followed all the steps mentioned, thanks in advance

This happens when an state has not been updated(loaded) by needed information so the value is null. To solve this, you may set initial value or put a condition to check if your array is not null(before assignment).

分别定义状态,这里您在静态导航中定义了状态,这是错误的

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM