Javascript 调用componentWillReceiveProps后,参数返回未定义

Javascript 调用componentWillReceiveProps后,参数返回未定义,javascript,reactjs,react-router,Javascript,Reactjs,React Router,我有一个React组件,当点击一个按钮时,它会使用组件将用户带到另一天的新URL,并且它应该呈现当天的事件/游戏(相同的组件,但应该获取新数据)。URL会更新,但是this.params.day-例如-返回未定义的,但是当使用React-Dev工具时,它在那里,我可以看到它,并且它已在第二天更新。。。我依靠React Router的参数来获取正确的数据,我有点困惑于为什么我不能让它正确地重新渲染组件。以下是该组件的重要部分: class Games extends Component {

我有一个React组件,当点击一个按钮时,它会使用
组件将用户带到另一天的新URL,并且它应该呈现当天的事件/游戏(相同的组件,但应该获取新数据)。URL会更新,但是
this.params.day
-例如-返回未定义的,但是当使用React-Dev工具时,它在那里,我可以看到它,并且它已在第二天更新。。。我依靠React Router的参数来获取正确的数据,我有点困惑于为什么我不能让它正确地重新渲染组件。以下是该组件的重要部分:

class Games extends Component {

  constructor(props){
    super(props);

    this.state = {
      loading: true,
      games: [],
      month: '',
      day: '',
      year: '',
      nextDay: '',
      prevDay: ''
    };

    // this.prevDay = this.prevDay.bind(this);
    // this.nextDay = this.nextDay.bind(this);
  }


  // set the state of the app based on the date
  parseDate(){
    // if we have params in the url
    if( this.props.params.month && this.props.params.day && this.props.params.year ){
      this.setState({
        month: moment(this.props.params.month, 'MM').format('MM'),
        day: moment(this.props.params.day, 'DD').format('DD'),
        year: moment(this.props.params.year, 'YYYY').format('YYYY'),
      });
    } else{
      // no params? set the date to today
      this.setState({
        month: moment().format('MM'),
        day: moment().format('DD'),
        year: moment().format('YYYY'),
      });
    }
    console.log('Date parsed.');
  }

  testProps(props){
    console.log('FIRED & Params: ' + this.props.params.day);
  }

  // generate previous day
  prevDay(){
    let currentDate = this.state.month+this.state.day+this.state.year,
        prevDate = moment(currentDate, 'MMDDYYYY').subtract(1, 'days').format('MM/DD/YYYY');
    this.setState({
      prevDay: prevDate
    });
  }

  // Generate next day
  nextDay(){
    let currentDate = this.state.month+this.state.day+this.state.year,
        nextDate = moment(currentDate, 'MMDDYYYY').add(1, 'days').format('MM/DD/YYYY');
    this.setState({
      nextDay: nextDate
    });
  }

  // Grab games for current day
  getGames(){

    this.setState({loading: true});

    // error handling
    function handleErrors(response) {
      if (!response.ok) {
        throw Error(response.statusText + 'POOP!');
      }
      return response;
    }

    fetch(`http://mlb.mlb.com/gdcross/components/game/mlb/year_${this.state.year}/month_${this.state.month}/day_${this.state.day}/master_scoreboard.json`)
    //fetch(`http://localhost:3000/inprogress.json`)
    .then(handleErrors)
    .then(response => response.json())
    .then(games => {
      const gamesLoaded = games.data;

      this.setState({
        games: gamesLoaded,
        loading: false
      });
    })
    .catch(error => this.setState({games: 'Error Finding Games'}) );
    console.log('Games fetched.');
  }

  componentWillMount(){
    this.parseDate();
    console.log('Component Will Mount Fired');
  }

  componentDidMount(){
    this.getGames();
    this.prevDay();
    this.nextDay();
    console.log('Component Mounted');
  }

  componentWillReceiveProps(){
    this.parseDate();
    // this.testProps();
    console.log(this.props.params.day);

    console.log('Component received props!');
  }
其中最重要的部分是
parseDate()
函数,因为它应该根据日期设置组件的状态,但在
componentWillReceiveProps
中调用时,参数未定义,但在React Dev工具中可见

任何想法和帮助都将不胜感激。谢谢

接受新道具的论证。你想要什么

componentWillReceiveProps(newProps) {
  this.parseDate(newProps)
}

并让
parseDate
使用参数而不是
this.props

啊,明白了,我可以看到它是如何工作的,我看到了它的价值。因此,在我现有的
parseDate
函数中,如果
newProps
存在,我是否只需要添加一个条件语句来处理?或者写一个新的函数来更新会更好吗?你有我能看到或写的好例子吗?感谢您向正确的方向推进。有几种方法可以做到这一点,我的方法是将道具传递到
parseDate
,而不是在那里使用
this.props
。因此,您的cWRP将像我的答案一样,在componentWillMount中,它将是
parseDate(this.props)
。我在打电话,所以我不能写很多代码