【问题标题】:this.setState not working properly; child props inherit wrong information - React-Nativethis.setState 无法正常工作;子道具继承错误信息 - React-Native
【发布时间】:2019-11-30 09:30:26
【问题描述】:

我对 React Native 比较陌生,我正在尝试创建自定义日历,但遇到了一些关于 this.setState 的问题。基本上,当我在 componentDidMount() 函数中有代码 this.setState 并打印正确的数字时;但是,当我在日历类中执行 console.log 时,它说我的月份是 0(初始值)。

我试过了:

  • 在构造函数中有一个函数(即 this.updateCurrentCalendar())并在构造函数中运行它(返回未安装错误)
  • 渲染前的ComponentDidMount()(当前版本)

这是我的 CalendarPage 代码:

  componentDidMount(){
    var tMonth =  parseInt(new Date().getMonth()+1);
    var tYear = parseInt(new Date().getFullYear());

    //ran ,()=>console.log after the first argument in this.setState and returns the correct month
    this.setState({trackMonth: tMonth, trackYear: tYear});
  }

  render(){
    return(
      <View style = {styles.CalendarViewStyle}>
        <Calendar style = {styles.CalendarStyle} thisMonth = {this.state.trackMonth} thisYear = {this.trackYear}/>
        <Button
          title = "Next"
          onPress = {()=>this.setState({trackMonth: this.state.trackMonth+1})}/>
        <Button
          title = "Back"
          onPress = {()=>this.setState({trackMonth: this.state.trackMonth-1})}/>
      </View>
    )
  }

这是我的日历代码:

  componentDidMount(){
    //console.log(this.state.monthStartDay) says it is 0 here*
    //finding the maximum day in a month
    var maxDay = this.findMaxDate(this.state.monthDate);
    this.setState({maxDate: maxDay});

    //finding which day does the first day of the path starts
    var startDay = this.findDay(1);
    this.setState({monthStartDay: startDay});

    //first week start day (if first day of the month does not start on monday)
    var fLineStartDate = this.findFirstLineStartDate();
    this.setState({firstLineStartDate: fLineStartDate});

    //second week start day
    var sLineStartDate = this.findSecondStartLine(fLineStartDate+7);
    this.setState({secondLineStartDate: sLineStartDate});
  }

  render(){
    var lastMaxDay = this.findMaxDate(this.state.monthDate-1);
    return(
      <View style = {styles.CalendarViewStyle}>
        <CalendarLine
          style = {styles.CalendarLineStyle}
          item = {{lineDay: this.state.firstLineStartDate, maxDay: this.state.maxDate, lineNum: 0, lastMaxDay: lastMaxDay}}/>
      </View>

非常感谢您的帮助!

编辑: 这是我在日历类中的构造函数

    constructor(props){
      super(props);
      this.state = {
        monthStartDay: 0,
        firstLineStartDate: 0,
        secondLineStartDate: 0,
        monthDate: this.props.thisMonth,
        yearDate: this.props.thisYear,
        maxDate:0,
      };
    this.findDay = this.findDay.bind(this);
    this.findMaxDate = this.findMaxDate.bind(this);
    this.findLeapYear = this.findLeapYear.bind(this);
    this.findFirstLineStartDate = this.findFirstLineStartDate.bind(this);
    this.findSecondStartLine = this.findSecondStartLine.bind(this);
  }

【问题讨论】:

  • 但是两个类中的变量不同。
  • 啊对。我在我的 this.state 中做了 this.props.thisMonth (即 this.state = {monthDate: this.props.thisMonth, ...})

标签: javascript reactjs react-native


【解决方案1】:

为什么不使用传递给 Calendar 类的道具而不将它们分配给您的状态构造函数

var lastMaxDay = this.findMaxDate(this.state.monthDate-1);

相当使用:

var lastMaxDay = this.findMaxDate(this.props.monthDate-1);

当您收到这些作为道具时,我假设您不会在 Calendar 类中对它们调用 setState?

通过将它们分配给日历类中的状态,您试图通过一次 setState 调用在 2 个不同的类中设置状态。

【讨论】:

  • 啊对,因为我要创建的日历能够在不同的月份和年份之间切换(因此第一页将显示当前年份和月份的日历;单击下一步按钮将显示下个月的日历);我对 React Native 比较陌生,我在某处读到我的动态数据应该处于状态。所以我试着把所有东西都放在状态
  • 我刚刚尝试过这样做,但它仍然无法正常工作......它仍然告诉我该值为0。我认为它没有更新状态的trackMonth......
【解决方案2】:
this.tMonth =  parseInt(new Date().getMonth()+1);
this.tYear = parseInt(new Date().getFullYear());

将其保存在构造函数中并将变量传递给日历组件

【讨论】:

  • 这暂时修复了 CalendarPage 中我必须将数据传递给 Calendar 的部分,但在 Calendar 中它仍然没有将数据传递给它的孩子。我刚刚做了一些测试,发现 componentDidMount 可以工作,但它只在传入错误数据后触发(即它加载 maxMonth=0 的数据,然后只加载 maxMonth =31 的数据)
猜你喜欢
  • 1970-01-01
  • 2020-02-28
  • 1970-01-01
  • 2018-06-13
  • 2021-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多