React-Native中的FlatList学习记录(二)之单个item刷新

React-Native中的FlatList学习记录(二)之单个item刷新

写作时间:2021/9/22
React-Native版本:0.63.2
目标平台:Android、iOS

FlatList数据过多的时候,通常希望能够针对某个需要修改的item进行局部刷新,下面举4个具体的实现方案,其中既包含可行方案也包含不可行的方案

方案一:直接修改某个item中的数据(不可行)

//App.js
'use strict';

import React from 'react';
import List from './demos/demo_1/List';

export default class App extends React.Component {
    render() {
        return (
            <List />
        );
    }
}
//List.js
'use strict';

import React from 'react';
import { FlatList, Button, SafeAreaView } from 'react-native';
import Item from './Item';

export default class List extends React.Component {
    constructor() {
        super();
        console.log('List_constructor()');

        this.state = {
            list: [
                { number: '11111111', data: { id: '1', name: 'aaa', age: 11 } },
                { number: '22222222', data: { id: '2', name: 'bbb', age: 22 } },
                { number: '33333333', data: { id: '3', name: 'ccc', age: 33 } },
            ],
        };
    }

    render() {
        console.log('List_render()');

        return (
            <SafeAreaView>
                <Button title={'button'} onPress={this.onButtonPress} />
                <FlatList data={this.state.list} renderItem={this.renderItem} keyExtractor={this.keyExtractor} />
            </SafeAreaView>
        );
    }

    renderItem = ({ item }) => {
        console.log('List_renderItem({item})', item);

        return <Item itemData={item} />;
    };

    keyExtractor = (item) => item.data.id;

    onButtonPress = () => {
        console.log('List_onButtonPress()');

        let tempList = this.state.list.concat([]);
        tempList[0].data.name = 'wtf';
        this.setState({ list: tempList });
    };
}
//Item.js
'use strict';

import React from 'react';
import { Text } from 'react-native';

export default class Item extends React.Component {
    constructor() {
        super();
        console.log('Item_constructor()');
    }

    shouldComponentUpdate(nextProps, nextState) {
        console.log('Item_shouldComponentUpdate(nextProps, nextState)');
        console.log('this.props.itemData:', JSON.stringify(this.props.itemData));
        console.log('nextProps.itemData:', JSON.stringify(nextProps.itemData));

        if (this.props.itemData.data.name !== nextProps.itemData.data.name) {
            return true;
        } else {
            return false;
        }
    }

    render() {
        console.log('Item_render()');

        return <Text>{this.props.itemData.data.name}</Text>;
    }
}

观察日志

LOG  List_constructor()
LOG  List_render()
LOG  List_renderItem({item}) {"data": {"age": 11, "id": "1", "name": "aaa"}, "number": "11111111"}
LOG  Item_constructor()
LOG  Item_render()
LOG  List_renderItem({item}) {"data": {"age": 22, "id": "2", "name": "bbb"}, "number": "22222222"}
LOG  Item_constructor()
LOG  Item_render()
LOG  List_renderItem({item}) {"data": {"age": 33, "id": "3", "name": "ccc"}, "number": "33333333"}
LOG  Item_constructor()
LOG  Item_render()
LOG  List_onButtonPress()
LOG  List_render()
LOG  List_renderItem({item}) {"data": {"age": 11, "id": "1", "name": "wtf"}, "number": "11111111"}
LOG  Item_shouldComponentUpdate(nextProps, nextState)
LOG  this.props.itemData: {"number":"11111111","data":{"id":"1","name":"wtf","age":11}}
LOG  nextProps.itemData: {"number":"11111111","data":{"id":"1","name":"wtf","age":11}}
LOG  List_renderItem({item}) {"data": {"age": 22, "id": "2", "name": "bbb"}, "number": "22222222"}
LOG  Item_shouldComponentUpdate(nextProps, nextState)
LOG  this.props.itemData: {"number":"22222222","data":{"id":"2","name":"bbb","age":22}}
LOG  nextProps.itemData: {"number":"22222222","data":{"id":"2","name":"bbb","age":22}}
LOG  List_renderItem({item}) {"data": {"age": 33, "id": "3", "name": "ccc"}, "number": "33333333"}
LOG  Item_shouldComponentUpdate(nextProps, nextState)
LOG  this.props.itemData: {"number":"33333333","data":{"id":"3","name":"ccc","age":33}}
LOG  nextProps.itemData: {"number":"33333333","data":{"id":"3","name":"ccc","age":33}}

列表实际并没有再次渲染,但是数据已经发生改变,shouldComponentUpdate的检测逻辑失效

方案二:通过shift和unshift方法调整数据列表(可行)

对onButtonPress方法进行调整

//List.js
'use strict';

import React from 'react';
import { FlatList, Button, SafeAreaView } from 'react-native';
import Item from './Item';

export default class List extends React.Component {
    constructor() {
        super();
        console.log('List_constructor()');

        this.state = {
            list: [
                { number: '11111111', data: { id: '1', name: 'aaa', age: 11 } },
                { number: '22222222', data: { id: '2', name: 'bbb', age: 22 } },
                { number: '33333333', data: { id: '3', name: 'ccc', age: 33 } },
            ],
        };
    }

    render() {
        console.log('List_render()');

        return (
            <SafeAreaView>
                <Button title={'button'} onPress={this.onButtonPress} />
                <FlatList data={this.state.list} renderItem={this.renderItem} keyExtractor={this.keyExtractor} />
            </SafeAreaView>
        );
    }

    renderItem = ({ item }) => {
        console.log('List_renderItem({item})', item);

        return <Item itemData={item} />;
    };

    keyExtractor = (item) => item.data.id;

    onButtonPress = () => {
        console.log('List_onButtonPress()');

        let tempList = this.state.list.concat([]);
        tempList.shift();
        tempList.unshift({
            number: '11111111',
            data: { id: '1', name: 'wtf', age: 11 },
        });
        this.setState({ list: tempList });
    };
}

观察日志

LOG  List_constructor()
LOG  List_render()
LOG  List_renderItem({item}) {"data": {"age": 11, "id": "1", "name": "aaa"}, "number": "11111111"}
LOG  Item_constructor()
LOG  Item_render()
LOG  List_renderItem({item}) {"data": {"age": 22, "id": "2", "name": "bbb"}, "number": "22222222"}
LOG  Item_constructor()
LOG  Item_render()
LOG  List_renderItem({item}) {"data": {"age": 33, "id": "3", "name": "ccc"}, "number": "33333333"}
LOG  Item_constructor()
LOG  Item_render()
LOG  List_onButtonPress()
LOG  List_render()
LOG  List_renderItem({item}) {"data": {"age": 11, "id": "1", "name": "wtf"}, "number": "11111111"}
LOG  Item_shouldComponentUpdate(nextProps, nextState)
LOG  this.props.itemData: {"number":"11111111","data":{"id":"1","name":"aaa","age":11}}
LOG  nextProps.itemData: {"number":"11111111","data":{"id":"1","name":"wtf","age":11}}
LOG  Item_render()
LOG  List_renderItem({item}) {"data": {"age": 22, "id": "2", "name": "bbb"}, "number": "22222222"}
LOG  Item_shouldComponentUpdate(nextProps, nextState)
LOG  this.props.itemData: {"number":"22222222","data":{"id":"2","name":"bbb","age":22}}
LOG  nextProps.itemData: {"number":"22222222","data":{"id":"2","name":"bbb","age":22}}
LOG  List_renderItem({item}) {"data": {"age": 33, "id": "3", "name": "ccc"}, "number": "33333333"}
LOG  Item_shouldComponentUpdate(nextProps, nextState)
LOG  this.props.itemData: {"number":"33333333","data":{"id":"3","name":"ccc","age":33}}
LOG  nextProps.itemData: {"number":"33333333","data":{"id":"3","name":"ccc","age":33}}

列表能够正常渲染,shouldComponentUpdate检测生效

方案三:通过map方法修改item中的某个字段(不可行)

对onButtonPress方法进行调整

'use strict';

import React from 'react';
import { FlatList, Button, SafeAreaView } from 'react-native';
import Item from './Item';

export default class List extends React.Component {
    constructor() {
        super();
        console.log('List_constructor()');
        this.state = {
            list: [
                { number: '11111111', data: { id: '1', name: 'aaa', age: 11 } },
                { number: '22222222', data: { id: '2', name: 'bbb', age: 22 } },
                { number: '33333333', data: { id: '3', name: 'ccc', age: 33 } },
            ],
        };
    }

    render() {
        console.log('List_render()');
        return (
            <SafeAreaView>
                <Button title={'button'} onPress={this.onButtonPress} />
                <FlatList data={this.state.list} renderItem={this.renderItem} keyExtractor={this.keyExtractor} />
            </SafeAreaView>
        );
    }

    renderItem = ({ item }) => {
        console.log('List_renderItem({item})', item);
        return <Item itemData={item} />;
    };

    keyExtractor = (item) => item.data.id;

    onButtonPress = () => {
        console.log('List_onButtonPress()');

        let tempList = this.state.list.concat([]);
        this.setState({
            list: tempList.map((item) => {
                if (item.data.name === 'aaa') {
                    item.data.name = 'wtf';
                }
                return item;
            }),
        });
    };
}
LOG  List_constructor()
LOG  List_render()
LOG  List_renderItem({item}) {"data": {"age": 11, "id": "1", "name": "aaa"}, "number": "11111111"}
LOG  Item_constructor()
LOG  Item_render()
LOG  List_renderItem({item}) {"data": {"age": 22, "id": "2", "name": "bbb"}, "number": "22222222"}
LOG  Item_constructor()
LOG  Item_render()
LOG  List_renderItem({item}) {"data": {"age": 33, "id": "3", "name": "ccc"}, "number": "33333333"}
LOG  Item_constructor()
LOG  Item_render()
LOG  List_onButtonPress()
LOG  List_render()
LOG  List_renderItem({item}) {"data": {"age": 11, "id": "1", "name": "wtf"}, "number": "11111111"}
LOG  Item_shouldComponentUpdate(nextProps, nextState)
LOG  this.props.itemData: {"number":"11111111","data":{"id":"1","name":"wtf","age":11}}
LOG  nextProps.itemData: {"number":"11111111","data":{"id":"1","name":"wtf","age":11}}
LOG  List_renderItem({item}) {"data": {"age": 22, "id": "2", "name": "bbb"}, "number": "22222222"}
LOG  Item_shouldComponentUpdate(nextProps, nextState)
LOG  this.props.itemData: {"number":"22222222","data":{"id":"2","name":"bbb","age":22}}
LOG  nextProps.itemData: {"number":"22222222","data":{"id":"2","name":"bbb","age":22}}
LOG  List_renderItem({item}) {"data": {"age": 33, "id": "3", "name": "ccc"}, "number": "33333333"}
LOG  Item_shouldComponentUpdate(nextProps, nextState)
LOG  this.props.itemData: {"number":"33333333","data":{"id":"3","name":"ccc","age":33}}
LOG  nextProps.itemData: {"number":"33333333","data":{"id":"3","name":"ccc","age":33}}

列表实际并没有再次渲染,但是数据已经发生改变,shouldComponentUpdate检测逻辑失效

方案四:通过map方法替换整个item(可行)

对onButtonPress方法进行调整

'use strict';

import React from 'react';
import { FlatList, Button, SafeAreaView } from 'react-native';
import Item from './Item';

export default class List extends React.Component {
    constructor() {
        super();
        console.log('List_constructor()');
        this.state = {
            list: [
                { number: '11111111', data: { id: '1', name: 'aaa', age: 11 } },
                { number: '22222222', data: { id: '2', name: 'bbb', age: 22 } },
                { number: '33333333', data: { id: '3', name: 'ccc', age: 33 } },
            ],
        };
    }

    render() {
        console.log('List_render()');
        return (
            <SafeAreaView>
                <Button title={'button'} onPress={this.onButtonPress} />
                <FlatList data={this.state.list} renderItem={this.renderItem} keyExtractor={this.keyExtractor} />
            </SafeAreaView>
        );
    }

    renderItem = ({ item }) => {
        console.log('List_renderItem({item})', item);
        return <Item itemData={item} />;
    };

    keyExtractor = (item) => item.data.id;

    onButtonPress = () => {
        console.log('List_onButtonPress()');

        let tempList = this.state.list.concat([]);
        this.setState({
            list: tempList.map((item) =>
                item.data.name === 'aaa' ? { ...item, data: { id: '1', name: 'wtf', age: 11 } } : item,
            ),
        });
    };
}
LOG  List_constructor()
LOG  List_render()
LOG  List_renderItem({item}) {"data": {"age": 11, "id": "1", "name": "aaa"}, "number": "11111111"}
LOG  Item_constructor()
LOG  Item_render()
LOG  List_renderItem({item}) {"data": {"age": 22, "id": "2", "name": "bbb"}, "number": "22222222"}
LOG  Item_constructor()
LOG  Item_render()
LOG  List_renderItem({item}) {"data": {"age": 33, "id": "3", "name": "ccc"}, "number": "33333333"}
LOG  Item_constructor()
LOG  Item_render()
LOG  List_onButtonPress()
LOG  List_render()
LOG  List_renderItem({item}) {"data": {"age": 11, "id": "1", "name": "wtf"}, "number": "11111111"}
LOG  Item_shouldComponentUpdate(nextProps, nextState)
LOG  this.props.itemData: {"number":"11111111","data":{"id":"1","name":"aaa","age":11}}
LOG  nextProps.itemData: {"number":"11111111","data":{"id":"1","name":"wtf","age":11}}
LOG  Item_render()
LOG  List_renderItem({item}) {"data": {"age": 22, "id": "2", "name": "bbb"}, "number": "22222222"}
LOG  Item_shouldComponentUpdate(nextProps, nextState)
LOG  this.props.itemData: {"number":"22222222","data":{"id":"2","name":"bbb","age":22}}
LOG  nextProps.itemData: {"number":"22222222","data":{"id":"2","name":"bbb","age":22}}
LOG  List_renderItem({item}) {"data": {"age": 33, "id": "3", "name": "ccc"}, "number": "33333333"}
LOG  Item_shouldComponentUpdate(nextProps, nextState)
LOG  this.props.itemData: {"number":"33333333","data":{"id":"3","name":"ccc","age":33}}
LOG  nextProps.itemData: {"number":"33333333","data":{"id":"3","name":"ccc","age":33}}

列表能够正常渲染,shouldComponentUpdate检测生效

你可能感兴趣的:(React-Native,javascript,react,native)