Material UI - 数组在 onChange 事件后重置为 0 个元素。 (文本字段组件)

发布于 2025-01-13 00:06:11 字数 2915 浏览 3 评论 0原文

我目前正在构建一个应用程序,我希望人们从用户列表中选择他们想要与之创建群组聊天室的用户。之后,我有一个文本字段,允许他们为其组设置名称。

问题: textfield 的 onChange 事件似乎在键入时将数组重置为 0 个元素。请参阅下面的 GIF。

有人遇到过这样的事情吗?我将在下面粘贴我的相关代码片段。

return (
        <div>
            <Dialog
                open={MultiplelistClick}
                keepMounted
                className='grpchatDialog'
                onClose={() => setMultiplelistClick(false)}
                aria-describedby="alert-dialog-slide-description"
                fullWidth
                maxWidth="xl"
            >
                <DialogTitle className='dialogTitle'>{"Choose users"}</DialogTitle>
                <DialogContent>
                    <DialogContentText className="dialog-information">
                        2 minimum users required here.
                    </DialogContentText>
                </DialogContent>
                {

                    userData && userData.filter((val: any) => {
                        return (searchTerm === '' && (val.id != myIdNumber) || (val.firstName.toLowerCase().includes(searchTerm.toLocaleLowerCase()) && (val.id != myIdNumber)));
                    }).map((user: UserModel) => {
                        return <li key={user.id}>
                            <img className='liAvatar' src={placeholderimg} alt='xx' />
                            <p className='nameDisplay'onClick={() => createChatRoom(user)}> {user.firstName} {user.lastName}</p>
                            <Checkbox value={user.id} onClick={() => CheckBoxEvent(user.id)} className='myCheckBox' />
                        </li>
                    })
                }
                <TextField
                    label='Give your group a name.'
                    variant='outlined'
                    rows={1}
                    className='grpNameInput'
                    onChange={(e) => setGroupName(e.target.value)}
                />
                <DialogActions>
                    <Button onClick={() => CreateGroupChatRoom(GroupChatList)}>Skapa Chattrum</Button>
                </DialogActions>
            </Dialog>
        </div>

更新:我验证了它实际上是我的TextField中的onChange输入事件,将我当前的用户ID数组重置为0。请在此处查看GIF:https://gyazo.com/f306b6907ca755ad07fa13b542b87d27

对于任何想知道我的事件如何将用户 ID 推入数组的人,以下是该事件:

const CheckBoxEvent = (userid: number) => {
    if (!GroupChatList.includes(userid)) {
        GroupChatList.push(userid); console.log(GroupChatList)
   }
    else {
            var index = GroupChatList.indexOf(userid);
            GroupChatList.splice(index, 1);
    }
}

Im currently building a application where i want people to choose from a list of users who they want to create a group chatroom with. After that, i have a TextField that allows them to set a name to their group.

The problem: The onChange event for the textfield seems to reset the array to 0 elements when typing. See GIF below.

Have anyone encountered something like this? I will paste my relevant code snippet down below.

return (
        <div>
            <Dialog
                open={MultiplelistClick}
                keepMounted
                className='grpchatDialog'
                onClose={() => setMultiplelistClick(false)}
                aria-describedby="alert-dialog-slide-description"
                fullWidth
                maxWidth="xl"
            >
                <DialogTitle className='dialogTitle'>{"Choose users"}</DialogTitle>
                <DialogContent>
                    <DialogContentText className="dialog-information">
                        2 minimum users required here.
                    </DialogContentText>
                </DialogContent>
                {

                    userData && userData.filter((val: any) => {
                        return (searchTerm === '' && (val.id != myIdNumber) || (val.firstName.toLowerCase().includes(searchTerm.toLocaleLowerCase()) && (val.id != myIdNumber)));
                    }).map((user: UserModel) => {
                        return <li key={user.id}>
                            <img className='liAvatar' src={placeholderimg} alt='xx' />
                            <p className='nameDisplay'onClick={() => createChatRoom(user)}> {user.firstName} {user.lastName}</p>
                            <Checkbox value={user.id} onClick={() => CheckBoxEvent(user.id)} className='myCheckBox' />
                        </li>
                    })
                }
                <TextField
                    label='Give your group a name.'
                    variant='outlined'
                    rows={1}
                    className='grpNameInput'
                    onChange={(e) => setGroupName(e.target.value)}
                />
                <DialogActions>
                    <Button onClick={() => CreateGroupChatRoom(GroupChatList)}>Skapa Chattrum</Button>
                </DialogActions>
            </Dialog>
        </div>

UPDATE: I verified it is actually the onChange typing event in my TextField that resets my current array of user ids to 0. See GIF here: https://gyazo.com/f306b6907ca755ad07fa13b542b87d27

For anyone wondering how my event for pushing in the user ids into the array, heres the event for that:

const CheckBoxEvent = (userid: number) => {
    if (!GroupChatList.includes(userid)) {
        GroupChatList.push(userid); console.log(GroupChatList)
   }
    else {
            var index = GroupChatList.indexOf(userid);
            GroupChatList.splice(index, 1);
    }
}

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

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

发布评论

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

评论(1

南笙 2025-01-20 00:06:11

React 频繁重新渲染组件。通过重新渲染,特别是对于功能组件,我们的意思是:“运行函数”...它每次都会重新创建声明的变量和函数。因此,当组件重新呈现时,GroupChatList 是一个新变量,重置为组件内的代码所说的任何内容。

要跨渲染维护变量中的值,请使用 useState 挂钩将变量置于“状态”中。

另外,当你使用“设置状态函数”来更新状态变量中的值时,这会触发 React 需要重新渲染组件(因为“状态中的信息已更改”)。

React re-renders components frequently. By re-render, especially with Functional Components, we mean: "run the Function"...which re-creates the declared variables and functions within each and every time. So when the component re-renders, GroupChatList is a new variable reset to whatever the code within the component says.

To maintain a value in a variable across renders, use the useState hook to put the variable into "state".

Also, when you use the "set state function" to update the value in the state variable, that is the trigger to React that it needs to re-render the component (because "information in state has changed").

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