首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >默认道具被忽略。

默认道具被忽略。
EN

Stack Overflow用户
提问于 2017-09-23 12:15:01
回答 3查看 3.7K关注 0票数 2

我在React中使用了Flow v.0.52类型,但在正确分配默认道具方面遇到了问题。

News.jsx

代码语言:javascript
复制
class News extends Component {
  static defaultProps = {
    postList: {apiData: [], total: '0'},
  };

  componentDidMount() {
    this.props.getPostListData(this.props.lang, null, this.props.category);
  }

  props: {
    postList: {apiData: Array<Content>, total: string},
    getPostListData: Function,
  };

  render() {
    const { postList } = this.props;
    let renderNewsCards;

    if (postList.apiData.length !== 0) {
      renderNewsCards = (
        <NewsCards ... />
      );
    } else {
      renderNewsCards = <p style={{ textAlign: 'center' }}>Loader...</p>;
    }
...

News.jsx组件中,忽略默认道具,结果postList.apiData.length遇到类型错误Uncaught TypeError: Cannot read property 'length' of undefined

News.jsx P.S. https://gist.github.com/Y-Taras/6cb04d51d7c0561f9396e562640d3bbf 提交

EN

Stack Overflow用户

发布于 2017-09-23 18:08:42

我觉得打字有些问题,我在流脚本方面没有多少经验。但下面的方法会有效的!你能试着回答吗?

代码语言:javascript
复制
import * as React from 'react'

type Content = number // assumed content type as number 
type Props = {
  postList: {apiData: Array<Content>, total: string},
  getPostListData: Function,
  lang: string,
  category: string
}

class News extends React.Component<Props> {
  static defaultProps = {
    postList: {apiData: [], total: '0'},
    lang: 'defaultLanguage',
    category: 'defaultCategory'
  }

  componentDidMount() {
    return this.props.getPostListData(this.props.lang, null, this.props.category)
  } 

  render() {
    const { postList } = this.props;

    return postList.apiData.length !== 0 ?
      <div> news Cards </div> :
      <p style={{ textAlign: 'center' }}>Loader...</p>
  }
}
票数 0
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46379467

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档