3
나는 백엔드 그래프 서비스에서 레코드를 가져 와서 Array.map 함수로 렌더링하려고합니다. 불행히도 그들이로드되기 전에 그들은 정의되지 않았기 때문에 오류가 발생합니다. 구성 요소에 기본 소품을 설정하려고했지만 작동하지 않습니다. 모든 것이로드되었는지 또는 해당 소품에 기본 값을 주입하는 특정 방법이 있는지 확인해야합니까? 내 코드는 지금과 같습니다.graphact를 사용하여 구성 요소 기본 소품을 응답하십시오.
import React from 'react';
import { graphql } from 'react-apollo';
import { fetchTasks } from '../../../graphql/tasks';
import { Dashboard } from '../components/Dashboard';
const propTypes = {
data: React.PropTypes.shape({
tasks: React.PropTypes.array
})
};
const defaultProps = {
data: {
tasks: []
}
};
class DashboardContainer extends React.Component {
render() {
const titles = this.props.data.tasks.map(task => task.title);
return(
<Dashboard
titles={titles}
/>
);
}
}
DashboardContainer.propTypes = propTypes;
DashboardContainer.defaultProps = defaultProps;
export default graphql(fetchTasks)(DashboardContainer);
예 당신은 당신의 소품이 당신이 원하는 값으로 업데이트되어 있는지 확인해야합니다. 그래서 당신은 this.props.data.tasks.length를 체크 할 필요가있다. –