Flatlist 不渲染对象数组

发布于 2025-01-10 15:56:59 字数 2287 浏览 0 评论 0原文

大家好,我正在尝试渲染一个对象列表来表示从自定义键盘中获取的一些字母。我的平面列表看起来像这样:

<FlatList
horizontal={true}
data = {keyList}
renderItem = {({item}) => 
    <View style={styles.singleBlock}>
        <Text style={styles.textBlock}>{item.letter}</Text>
    </View>
    }
keyExtractor = {myKey}
/>

数据取自我的状态:

    const [keyList, setKeyList] = useState([
        {
            'letter': "",
            'state': "nonIndovinato",
            'id': 1
        },
        {
            'letter': "",
            'state': "Indovinato",
            'id': 2
        },
        {
            'letter': "",
            'state': "Indovinato",
            'id': 3
        },
        {
            'letter': "",
            'state': "Indovinato",
            'id': 4
        },
        {
            'letter': "",
            'state': "Indovinato",
            'id': 5
        }
    ]);

并通过更新函数更新:

    const updateData = (key) => {
        const index = keyList.findIndex(item => item.id === key.id);

        if(index === -1) return;

        const item = keyList[index];

        const updatedItem = {...item, letter: key.letter};

        const updatedArray = keyList;

        updatedArray[index] = updatedItem;

        setKeyList(updatedArray);
    };

键值如下:

{
    'letter': 'A',
    'id': 1
}

我在 console.log 中看到的数据是正确的,例如,如果我按 T,它看起来像这样:

{letter: 'T', state: 'nonIndovinato', id: 1},
{letter: '', state: 'Indovinato', id: 2},
{letter: '', state: 'Indovinato', id: 3},
{letter: '', state: 'Indovinato', id: 4},
{letter: '', state: 'Indovinato', id: 5}

应用程序如下所示: https://i.sstatic.net/JhQiq.jpg 上部是应显示数据的结构,下部是模型数据的预期结果。

如果您需要这种风格:

    singleBlock: {
        backgroundColor: 'grey',
        width: width/6,
        justifyContent: 'center',
        alignItems: 'center',
        margin: 1,
        marginTop: '5%',
        marginBottom: '5%',
        marginRight: 2,
        borderRadius: 1
    },
    textBlock: {
        color: 'white',
        fontSize: width/10,
        fontWeight: 'bold'
    }

感谢任何可以提供帮助的人:)

Hello everybody i'm trying to render a list of objects to rapresent some letters, taken from a custom keyboard. my flatlist look like this:

<FlatList
horizontal={true}
data = {keyList}
renderItem = {({item}) => 
    <View style={styles.singleBlock}>
        <Text style={styles.textBlock}>{item.letter}</Text>
    </View>
    }
keyExtractor = {myKey}
/>

the data is taken from my state:

    const [keyList, setKeyList] = useState([
        {
            'letter': "",
            'state': "nonIndovinato",
            'id': 1
        },
        {
            'letter': "",
            'state': "Indovinato",
            'id': 2
        },
        {
            'letter': "",
            'state': "Indovinato",
            'id': 3
        },
        {
            'letter': "",
            'state': "Indovinato",
            'id': 4
        },
        {
            'letter': "",
            'state': "Indovinato",
            'id': 5
        }
    ]);

and updated by update function:

    const updateData = (key) => {
        const index = keyList.findIndex(item => item.id === key.id);

        if(index === -1) return;

        const item = keyList[index];

        const updatedItem = {...item, letter: key.letter};

        const updatedArray = keyList;

        updatedArray[index] = updatedItem;

        setKeyList(updatedArray);
    };

the key value is something as follow:

{
    'letter': 'A',
    'id': 1
}

the data i see with console.log is right, for example if i press T it look like this:

{letter: 'T', state: 'nonIndovinato', id: 1},
{letter: '', state: 'Indovinato', id: 2},
{letter: '', state: 'Indovinato', id: 3},
{letter: '', state: 'Indovinato', id: 4},
{letter: '', state: 'Indovinato', id: 5}

the application look like this: https://i.sstatic.net/JhQiq.jpg
the upper part is the structure where it should be displayed the data, the lower is the expected result with mockup data.

if you need the style:

    singleBlock: {
        backgroundColor: 'grey',
        width: width/6,
        justifyContent: 'center',
        alignItems: 'center',
        margin: 1,
        marginTop: '5%',
        marginBottom: '5%',
        marginRight: 2,
        borderRadius: 1
    },
    textBlock: {
        color: 'white',
        fontSize: width/10,
        fontWeight: 'bold'
    }

Thanks to anyone who can help :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新雨望断虹 2025-01-17 15:56:59

您更新状态的方式不正确。尝试以下操作。

const updateData = (key) => {
        const index = keyList.findIndex(item => item.id === key.id);

        if(index === -1) return;

        const item = keyList[index];
        const updatedItem = {...item, letter: key.letter};


        let updatedArray = [...keyList];
        updatedArray[index] = updatedItem;
        
        setKeyList(updatedArray);
};

The way you are updating your state is not correct. Try the following.

const updateData = (key) => {
        const index = keyList.findIndex(item => item.id === key.id);

        if(index === -1) return;

        const item = keyList[index];
        const updatedItem = {...item, letter: key.letter};


        let updatedArray = [...keyList];
        updatedArray[index] = updatedItem;
        
        setKeyList(updatedArray);
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文