导出默认使用选择

时间:2018-01-22 17:37:55

标签: javascript reactjs

道歉,如果我的问题听起来很愚蠢,但我是是一个菜鸟: - / 我在github file找到了一些奇怪的东西。 我知道我们可以导出或导入默认功能但在这里 作者使用:

export default withAPIData( ( props ) => {
    const { postsToShow, order, orderBy, categories } = props.attributes;
    const queryString = stringify( pickBy( {
        categories,
        order,
        orderBy,
        per_page: postsToShow,
        _fields: [ 'date_gmt', 'link', 'title' ],
    }, value => ! isUndefined( value ) ) );
    return {
        latestPosts: `/wp/v2/posts?${ queryString }`,
    };
} )( LatestPostsBlock );

但他使用

在render()中使用latestPosts: /wp/v2/posts?${ queryString }获得的内容
const latestPosts = this.props.latestPosts.data;

我不明白为什么要使用导出默认值,如果他想使用他导致同一个文件。

1 个答案:

答案 0 :(得分:0)

他没有导出名为" withAPIData"的东西,他将该函数的结果导出为"默认"。

这是等效的:

const ConnectedLatestPostsBlock = withAPIData( ( props ) => {
  const { postsToShow, order, orderBy, categories } = props.attributes;
  const queryString = stringify( pickBy( {
    categories,
    order,
    orderBy,
    per_page: postsToShow,
    _fields: [ 'date_gmt', 'link', 'title' ],
  }, value => ! isUndefined( value ) ) );
  return {
    latestPosts: `/wp/v2/posts?${ queryString }`,
  };
} )( LatestPostsBlock );

export default ConnectedLatestPostsBlock;

他将他的组件(LastestPostsBlock)传递给HOC (High Order Component),它在渲染时克隆组件并向下传递更多道具。在这种情况下,道具"数据"。

相关问题