我有Reminder component
一个表单,我在其中存储文本和日期 onClick 使用AsyncStorage
.
现在,我想在Agenda Component
.
我正在使用Agenda component
库react-native-calendars
react -native-calendars
这是我的reminder component
class Reminder extends Component {
constructor(props) {
super(props);
this.state = {
input: '',
chosenDate: new Date(),
};
this.setDate = this.setDate.bind(this);
this.handleChangeInput = this.handleChangeInput.bind(this);
this.saveData = this.saveData.bind(this);
}
setDate(newDate) {
this.setState({
chosenDate: newDate
});
}
handleChangeInput = (text) => {
this.setState({input:text});
}
//save the input
saveData() {
AsyncStorage.setItem("key", JSON.stringify(this.state));
}
render() {
return (
<View>
<Form style={styles.formContainer}>
<View style={styles.formView}>
< TextInput
placeholder = "Set your reminder"
onChangeText={this.handleChangeInput}
value={this.state.input}
/>
<DatePicker
defaultDate={new Date()}
minimumDate={new Date(2018, 1, 1)}
maximumDate={new Date(2019, 12, 31)}
locale={"en"}
timeZoneOffsetInMinutes={undefined}
modalTransparent={false}
animationType={"fade"}
androidMode={"default"}
placeHolderText="Select date"
textStyle={{ color: "green" }}
placeHolderTextStyle={{ color: "#d3d3d3" }}
onDateChange={this.setDate}
/>
<Text style={styles.datePicker}>
{this.state.chosenDate.toString().substring(0,10)}
</Text>
</View>
<View style={styles.footer}>
<Button block success style={styles.saveBtn}
onPress={ () =>
{
this.saveData()
console.log('save data',this.state);
}
}
>
<Icon type='MaterialIcons' name='done' />
</Button>
</View>
</Form>
</View>
);
}
}
export default Reminder;
这是屏幕Reminder screen
import React, { Component } from 'react';
import { View, StatusBar } from 'react-native';
import PropTypes from 'prop-types';
import Reminder from '../components/Reminder';
const ReminderScreen = ({navigation}) => (
<View >
<Reminder navigation={navigation} >
<StatusBar backgroundColor = "#28F1A6" />
</Reminder >
</View>
);
Reminder.propTypes = {
navigation: PropTypes.object.isRequired
}
export default ReminderScreen;
这是我要显示该数据的组件Agenda Component
class WeeklyAgenda extends Component {
constructor(props) {
super(props);
this.state = {
items: {},
selectedDate: ''
};
}
render() {
return (
<View style={{height:600}}>
<Agenda
items={this.state.items}
loadItemsForMonth={this.loadItems.bind(this)}
selected={this.props.day}
renderItem={this.renderItem.bind(this)}
renderEmptyData={this.renderEmptyDate.bind(this)}
rowHasChanged={this.rowHasChanged.bind(this)}
onRefresh = {() => { this.setState({refeshing : true})}}
refreshing = {this.state.refreshing}
refreshControl = {null}
pastScrollRange={1}
futureScrollRange = {3}
theme = {
{
agendaTodayColor: '#28F1A6',
agendaKnobColor: '#28F1A6',
dotColor: '#28F1A6',
selectedDayBackgroundColor: '#28F1A6',
todayTextColor: '#28F1A6',
}
}
/>
<View >
<Fab
active={!this.state.active}
direction="up"
style={{ backgroundColor: '#28F1A6'}}
position = 'bottomRight'
onPress={() => this.props.navigation.navigate('Reminder')}>
<Icon type='MaterialCommunityIcons' name="reminder" />
</Fab>
</View>
</View>
);
}
//On application loads, this will get the already saved data and set the state true when it's true.
componentDidMount() {
AsyncStorage.getItem("key").then((newItems) => {
this.setState(JSON.parse(newItems));
});
}
loadItems = (day) => {
console.log('day',day);
console.log('items', this.state.items);
const {selectedDate} = this.state;
setTimeout(() => {
console.log('selected date', selectedDate);
this.setState({selectedDate: day});
console.log('selected date later', day);
const newItems = {};
Object.keys(this.state.items).forEach(key => {newItems[key] = this.state.items[key];});
console.log('new items later', newItems);
this.setState({
items: newItems
});
console.log('new items later', this.state.newItems);
console.log('items later', this.state.items);
this.state.items;
},1000);
};
renderItem(item) {
return (
<View style={[styles.item, {height: item.height}]}>
<TouchableOpacity onPress={() => {this.props.navigation.navigate('Reminder')}}>
<Text>{item.name}</Text>
</TouchableOpacity>
</View>
);
}
renderEmptyDate() {
return (
<View style={styles.emptyDate}>
<TouchableOpacity onPress={() => {this.props.navigation.navigate('Reminder')}}>
<Text style={styles.emptyTextColor}> No Event or Reminder on this date </Text>
</TouchableOpacity>
</View>
);
}
rowHasChanged(r1, r2) {
return r1.name !== r2.name;
}
timeToString(time) {
const date = new Date(time);
return date.toISOString().split('T')[0];
}
}
export default WeeklyAgenda;
这是它的屏幕Agenda Screen
import React, { Component } from 'react';
import { View, Text, StatusBar } from 'react-native';
import PropTypes from 'prop-types';
import WeeklyAgenda from '../components/Agenda';
class AgendaScreen extends Component {
state = { }
render() {
const {navigation} = this.props;
const { params } = this.props.navigation.state;
return (
<View style={{height: 100}}>
<WeeklyAgenda day={params["day"]} navigation={navigation}>
<StatusBar backgroundColor="#28F1A6" />
</WeeklyAgenda >
</View>
);
}
}
WeeklyAgenda.propTypes = {
navigation: PropTypes.object.isRequired
}
export default AgendaScreen;
我对 react-native 还很陌生,并且仍在尝试弄清楚如何在组件和屏幕之间共享数据。