【问题标题】:React-Native: how to bind item in component with item in another component?React-Native:如何将组件中的项目与另一个组件中的项目绑定?
【发布时间】:2019-05-06 09:48:38
【问题描述】:

我正在尝试使用react-native-calendars library by wix 构建日历。 例如,当我在 Calendar Component 中单击 2018 年 12 月 5 日时,它应该在 Agenda Component 上导航到 2018 年 12 月 5 日。这就是我想要做的。

这是我的 Calendar Component 代码,我试图在其中获取 id 并绑定它:

    class Calendar extends Component {
    constructor(props) {
        super(props);
         this.state = {
             active: 'true'
         }
    }

    render() { 
        const {onSelect} = this.props;
        return ( 
            <View>
                <CalendarList 
                    keyExtractor = {day=>day.id}
                    onDayPress={(day) => {onSelect.bind(this, day)}}
                    pastScrollRange={12}
                    futureScrollRange={12}
                    scrollEnabled={true}
                    showScrollIndicator={true}
                />
            </View>
         );
    }
}

Calendar.propTypes = {
    onSelect: PropTypes.func,
}

这是我的代码CalendarScreen,我在其中进行导航。

const CalendarScreen = ({navigation}) => (
    <View >
        <Calendar  navigation={navigation} 
            onSelect={(id)=>{
            navigation.navigate('Agenda', {id}) }}>
            <StatusBar backgroundColor="#28F1A6" />
        </Calendar >
    </View>
);

Calendar.propTypes = {
    navigation: PropTypes.object.isRequired
}

export default CalendarScreen;

我也提到了here的解决方案。但没有运气。

编辑:

这是我的Agenda Component

    class WeeklyAgenda extends Component {
    constructor(props) {
    super(props);
    this.state = {
      items: {}
    };
  }

  render() {
    return (
        <View style={{height:600}}>
             <Agenda
                items={this.state.items}
                loadItemsForMonth={this.loadItems.bind(this)}
                selected={Date()}
                renderItem={this.renderItem.bind(this)}
                renderEmptyDate={this.renderEmptyDate.bind(this)}
                rowHasChanged={this.rowHasChanged.bind(this)}
                onRefresh = {() => { this.setState({refeshing : true})}}
                refreshing = {this.state.refreshing}
                refreshControl = {null}
                pastScrollRange={1}
                futureScrollRange = {3}
            />
    );
  }

这是AgendaScreen

    const AgendaScreen = ({navigation}) => (
    <View style={{height: 100}}>
        <WeeklyAgenda  navigation={navigation}>
            <StatusBar backgroundColor="#28F1A6" />
        </WeeklyAgenda >
    </View>
);

WeeklyAgenda.propTypes = {
    navigation: PropTypes.object.isRequired
}

export default AgendaScreen;

【问题讨论】:

  • 您是否收到错误消息?
  • @RachelGallen 不,没有错误。

标签: javascript reactjs react-native bind react-navigation


【解决方案1】:

Calender 向 CalenderScreen 传递一个回调 onSelect(id)。

日历屏幕只需要调用onSelect(day)。

onDayPress={(day) => {onSelect(day)}}

或者让回调同时接受导航对象和日期

onDayPress={(day) => {onSelect(day, navigation)}}

现在应该设置日历

onSelect={(day, navigation)=>{
        navigation.navigate('Agenda', {day}) }}>

或者当您将导航对象传递给 Calender 时,为什么要传递回调

onDayPress={(day) => {this.props.navigation.navigate('Agenda', {day})}}

编辑:新课程,并将 id 更改为 day

同样的问题。 onSelect 导航到 AgentScreen 并将导航道具(其中包含日期对象)传递给它,然后,AgentScreen 将导航道具传递给 WeeklyAgent,但 WeeklyAgent 不使用导航道具来获取 id。

selected={Date()}

用今天的日期创建一个新的日期对象。 要获得正确的日期,您需要从导航中获取日期,通常类似于

this.props.navigation.state.params["day"]

返回过去的日期。

现在,如果 WeeklyAgenda 实际上不进一步使用导航,我就不会通过它。

每周议程应该有一个道具“日”,所以不是

 <WeeklyAgenda  navigation={navigation}>

试试

const { params } = this.props.navigation.state;
...
<WeeklyAgenda  day={params["day"}>

第一行将 params 对象从 state 解包到一个名为 params 的变量中。

WeeklyAgenda 现在可以了

selected={this.props.day}

我认为这里的问题根本不在于绑定。 我建议阅读此article 或其他有关绑定功能的内容。

【讨论】:

  • 感谢您的回复。使用这两种方法中的任何一种,我都可以从CalendarScreen 导航到AgendaScreen,但我无法说,从CalendarScreen 的12 月20 日到AgendaScreen. 的12 月20 日它确实导航但只是将我带到当前日期.
  • 不看 AgendaScreen,很难说出原因。你确定你是通过正确的一天吗?在 AgendaScreen 中打印导航中的 id。 AgendaScreen 是否使用 id 来设置日期?
  • 对不起,我绝对是 react-native 的初学者。我用Agenda ComponentAgendaScreen 更新了我的描述。我显然错过了一些东西,但不确定到底是什么
  • WeeklyAgenda 确实进一步使用导航。它导航到带有所选日期的提醒页面。
  • 你可以传递一个回调,比如 onDayPress
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-29
  • 2018-07-01
相关资源
最近更新 更多