2

这开始变得非常令人沮丧。基本上,我无法访问props我的子组件。如果我尝试使用 - 直接渲染它们this.props,它可以工作,但如果我需要对它们进行额外的处理,或者将它们保存到state,我总是得到未定义的道具。我有一个父组件,它看起来像这样:

import React from 'react';

import Title from './EventSubComponents/Title';
import SessionInfo from './EventSubComponents/SessionInfo';
import SessionTime from './EventSubComponents/SessionTime';
import Location from './EventSubComponents/Location';
import Subscribers from './EventSubComponents/Subscribers';

class EventNode extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
      'event': [],
    }
  }

  componentDidMount() {
    this.getEvent(this.props.location.selectedEventId);
  }

  getEvent(eventId) {
    fetch('/api/v.1.0/event/' + eventId, {mode: 'no-cors'})
      .then(function(response) {
        if(!response.ok) {
          console.log('Failed to get single event.');
          return;
        }
        return response.json();
      })
      .then((data) => {
        if (!data) {
          return;
        }
        this.setState({
          'event': data
        })
      });
  }

  render() {
    return(
      <div className="event-wrapper">
        <Title 
        title = { this.state.event.title } 
        date = { this.state.event.start }
        />
        <SessionInfo 
          distance = { this.state.event.distance }
          type = { this.state.event.type }
        />
        <SessionTime 
          start = { this.state.event.start }
          end = { this.state.event.end }
        />
        <Location location = { this.state.event.start_location }/>
        <Subscribers 
        subscribers = { this.state.event.subscribers } 
        eventId = { this.state.event._id }
        />
      </div>
    );
  }
}

export default EventNode;

还有我的子组件SessionTime,看起来像这样:

import React from 'react';
import moment from 'moment';

class Title extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      'title': '',
      'date': '',
    }
  }

  componentDidMount() {
    console.log(this.props.title);
    console.log(this.props.date);
    // undefined both props.
    this.convertToTitleDate(this.props.date);
    this.setState({
      'title': this.props.title
    })
  }

  convertToTitleDate(date) {
    var newDate = moment(date).format('dddd, Do MMMM')
    this.setState({
      'date': newDate,
    });
  }

  render() {
    return (
      <div className="event-title-wrapper">
        <h1> { this.state.title } </h1>
        <div className="event-title-date"> { this.state.date } </div>
      </div>
    );
  }
}

export default Title;

谁能解释一下,为什么在我的函数中两者this.props.datethis.props.title未定义componentDidMount?我的组件中有更多组件,我EventNode也有同样的问题。

更改componentDidMountcomponentWillMount无济于事。我相当确定我的父EventNode组件有问题,但我不知道在哪里。里面EventNode render()所有的状态变量都被定义了。

4

1 回答 1

2

您初始化event为一个空数组并向下传递this.state.event.startthis.state.event.endSessionTime这将undefined在第一次渲染时出现,因为event尚未加载并且数组上没有startend属性。

您可以改为设置eventnull最初,然后null从渲染方法返回,直到event加载。

例子

class EventNode extends React.Component {
  state = {
    event: null
  };

  // ...

  render() {
    const { event } = this.state;

    if (event === null) {
      return null;
    }

    return (
      <div className="event-wrapper">
        <Title title={event.title} date={event.start} />
        <SessionInfo distance={event.distance} type={event.type} />
        <SessionTime start={event.start} end={event.end} />
        <Location location={event.start_location} />
        <Subscribers
          subscribers={event.subscribers}
          eventId={this.state.event._id}
        />
      </div>
    );
  }
}
于 2019-02-17T15:05:02.490 回答