博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
关于Relay的麻烦之处
阅读量:4610 次
发布时间:2019-06-09

本文共 2697 字,大约阅读时间需要 8 分钟。

问题背景

由于QueryRender是直接将数据塞进Render()里的

handleUpdate = (hasNextPage, xdata) =>{    console.log(3);    const data = this.state.data.concat(xdata);    this.setState({      data: data,      loadingMore: false,      hasNextPage: hasNextPage      }, () => {        window.dispatchEvent(new Event('resize'));      });  }  render(){    return(      
{ if (error) { console.log(error) } if (!props) { return (
) } this.handleUpdate(props.bookList.hasNextPage, props.bookList.edges); const loadMore = this.state.hasNextPage ? (
{this.state.loadingMore &&
} {!this.state.loadingMore &&
}
) : null; const mydata = this.state.data.concat(props.bookList.edges); return (
) }} /> ) }

直接在render里进行setState会导致组件无限循环渲染,当然把queryrender取缔掉用fetch替换可以解决,但是怎么在使用relay的同时直接setState呢?

改进一:

export default class SearchList extends PureComponent{  state={    after: "",    data: [],  }  updateAfter = (after, xdata) =>{    const data = this.state.data.concat(xdata);    this.setState({after: after, data: data},    () =>{      window.dispatchEvent(new Event('resize'));    });  }  render(){    return(      
{ if (error) { console.log(error) } return (
this.updateAfter(props.bookList.pageInfo.endCursor, props.bookList.edges)} hasNextPage={props ? props.bookList.pageInfo.hasNextPage : null} dataSource={props ? this.state.data.concat(props.bookList.edges) : this.state.data}/> ) }} /> ) }}class SearchListComponent extends PureComponent{ constructor(props){ super(props) } componentWillReceiveProps = (nextProps) =>{ console.log(1) window.dispatchEvent(new Event('resize')); } render(){ const loadMore = this.props.hasNextPage ? (
{this.props.loadingMore &&
} {!this.props.loadingMore &&
}
) : null; return(
(
}>
{item.node.summary.replace(/
/g, ' ')}
{item.node.author}
|
{item.node.clickTimes} 点击
} />
)} /> ) } }

缺陷:点击加载更多会闪一下,因为render会走两遍,第一遍是加载中,return null

转载于:https://www.cnblogs.com/jiajin/p/8556113.html

你可能感兴趣的文章
HDU 3572 Task Schedule(拆点+最大流dinic)
查看>>
HDU——1405The Last Practice(试手map)
查看>>
PAT 基础编程题 4-11 求自定类型元素序列的中位数(希尔排序)
查看>>
PHP implode() 函数由数组合并字符串
查看>>
软件测试的生命周期&测试流程
查看>>
深入SpringMVC视图解析器
查看>>
hi~大家好,特地出来解释下最近为啥都不更新了!
查看>>
python linux 开启 一个新端口 查看文件
查看>>
ubuntu 常用命令
查看>>
Linux下命令行中的复制和粘贴
查看>>
mysql 使用的三个小技巧
查看>>
【2017-4-2】JS导航栏 选项卡
查看>>
转发:原本优秀的我们是怎样滑向平庸的
查看>>
命令密码MySQL忘记密码恢复密码的实现方法
查看>>
domino服务器繁忙时压缩操作不立即执行问题
查看>>
【搜狐驾校】手动更安全 如何换档最合理
查看>>
Command 和 Observer 的一些区别
查看>>
React——高阶组件
查看>>
关注博客
查看>>
OpenNI depth深度数据的数据格式
查看>>