1- import React , { Component } from 'react' ;
1+ import React from 'react' ;
22import { FlatList , FlatListProps , Text , StyleProp , ViewStyle , View } from 'react-native' ;
33import Item , { ListItemProps } from './Item' ;
44
@@ -43,7 +43,7 @@ export interface ListState {
4343 data : ListProps [ 'data' ] ;
4444}
4545
46- export default class List extends Component < ListProps , ListState > {
46+ export default class List extends React . PureComponent < ListProps , ListState > {
4747 static Item = Item ;
4848 static defaultProps : ListProps = {
4949 data : [ ] ,
@@ -57,27 +57,25 @@ export default class List extends Component<ListProps, ListState> {
5757 data : [ ] ,
5858 } ;
5959 }
60- getData ( nextProps ?: ListProps ) {
61- const { size, extra, paddingLeft, children } = nextProps || this . props ;
62- const dataSource = React . Children . toArray ( children )
63- . map ( ( child : React . ReactNode ) => {
64- if ( ! React . isValidElement ( child ) ) {
65- return null ;
66- }
67- const props = { size, ...child . props } ;
68- return React . cloneElement ( < Item paddingLeft = { paddingLeft } extra = { extra } { ...props } /> ) ;
69- } )
70- . filter ( Boolean ) ;
71- this . setState ( { data : dataSource as ListProps [ 'data' ] } ) ;
72- }
73- componentDidMount ( ) {
74- if ( ! this . props . renderItem ) {
75- this . getData ( ) ;
76- }
77- }
78- UNSAFE_componentWillReceiveProps ( nextProps : ListProps ) {
79- if ( nextProps !== this . props ) {
80- this . getData ( nextProps ) ;
60+ static getDerivedStateFromProps ( props : ListProps ) {
61+ const getData = ( ) => {
62+ const { size, extra, paddingLeft, children } = props ;
63+ const dataSource = React . Children . toArray ( children )
64+ . map ( ( child : React . ReactNode ) => {
65+ if ( ! React . isValidElement ( child ) ) {
66+ return null ;
67+ }
68+ const props = { size, ...child . props } ;
69+ return React . cloneElement ( < Item paddingLeft = { paddingLeft } extra = { extra } { ...props } /> ) ;
70+ } )
71+ . filter ( Boolean ) ;
72+ return dataSource ;
73+ } ;
74+ if ( ! props . renderItem ) {
75+ const result = getData ( ) ;
76+ return {
77+ data : result ,
78+ } ;
8179 }
8280 }
8381 renderItemChild ( props : ListRenderItemInfoCustom < { } > ) : React . ReactElement {
0 commit comments