因爲QueryRender是直接將數據塞進Render()裏的node
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( <QueryRenderer environment={env} query={SearchListQuery} variables={{ search: this.props.search, first: this.props.pageSize, after: this.state.after }} render={({error, props}) => { if (error) { console.log(error) } if (!props) { return (<Spin className={"selection-spin"} size={'large'}/>) } this.handleUpdate(props.bookList.hasNextPage, props.bookList.edges); const loadMore = this.state.hasNextPage ? ( <div style={{ textAlign: 'center', marginTop: 12, height: 32, lineHeight: '32px' }}> {this.state.loadingMore && <Spin />} {!this.state.loadingMore && <Button onClick={() => { this.setState({ loadingMore: true, after: props.bookList.pageInfo.endCursor}); }}>加載更多</Button>} </div> ) : null; const mydata = this.state.data.concat(props.bookList.edges); return ( <SearchListComponent loadMore={loadMore} dataSource={mydata}/> ) }} /> ) }
直接在render裏進行setState會致使組件無限循環渲染,固然把queryrender取締掉用fetch替換能夠解決,可是怎麼在使用relay的同時直接setState呢?fetch
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( <QueryRenderer environment={env} query={SearchListQuery} variables={{ search: this.props.search, first: this.props.pageSize, after: this.state.after }} render={({error, props}) => { if (error) { console.log(error) } return ( <SearchListComponent loading={!props && this.state.after== ""} loadingMore={!props} updateAfter={() => 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 ? ( <div style={{ textAlign: 'center', margin: 12, height: 32, lineHeight: '32px' }}> {this.props.loadingMore && <Spin />} {!this.props.loadingMore && <Button onClick={() =>{ this.props.updateAfter(); }}>加載更多</Button>} </div> ) : null; return( <List itemLayout="horizontal" loading={this.props.loading} loadMore={loadMore} dataSource={this.props.dataSource} grid={{ gutter: 24, xs: 1, sm: 1, md: 1, lg: 1, xl: 1, xxl: 1}} renderItem={item=> ( <List.Item> <a href={`/info/${item.node.bookId}`}> <Card hoverable bordered={false} className={"book-list" } cover={<img alt={item.node.bookName} src={item.node.cover} />}> <Meta title={item.node.bookName} description={ <div> <div className="book-list-summary" >{item.node.summary.replace(/<br>/g, ' ')}</div> <div className="book-list-info"><span>{item.node.author}</span><span className="split">|</span><span style={{color: 'red'}}>{item.node.clickTimes}</span> 點擊</div> </div> } /> </Card> </a> </List.Item> )} /> ) } }
缺陷:點擊加載更多會閃一下,由於render會走兩遍,第一遍是加載中,return nullthis