reactjs - React,如何将用户分配给按钮,以阻止用户?
问题描述
我得到了用户列表,每个用户旁边都有一个阻止按钮。单击阻止按钮后,会弹出一个模式要求确认。当我确认时,特定用户被阻止。
现在我得到了显示模式,但点击确认按钮后没有任何反应。我想我需要为按钮分配一个用户?解锁按钮有效,但它不在模式中。
我的代码:
import MyModal from '@/views/MyModal'
function UsersList({ users, userId }) {
function locking(pk, action) {
axios.get(`/user/${pk}/${action}/`).then(() => {
update()
})
}
return (
...{users != null && users.length > 0 ? (
users.map((profile) => {
return (
<tr key={profile.id} id={profile.id} className={userId === profile.id ? 'table-active' : ''}>
{showEntities && <td className='align-middle'>{profile.entity_name}</td>}
<td className='align-middle'>
{profile.first_name} {profile.last_name}
{!profile.active && (
<span className='ml-xl-2 badge badge-pill badge-secondary'>
<i className='fa fa-lock' /> Blocked
</span>
)}
</td>...
{profile.id !== userId && //to be sure to not block myself
(profile.active ? (
<button
type='button'
className='btn d-block btn-danger w-5rem mb-2 badge'
data-toggle='modal'
data-target='#MyModal'
>
Block
</button>
) : (
<a
className='btn d-block btn-warning w-5rem mb-2 badge'
href='#'
onClick={() => {
locking(profile.id, 'unlock')
}}
>
Unblock
</a>
))}
</td>
</tr>
)
})
) : (
)}
</tbody>
</table>
<MyModal locking={locking()} />
</div>
)
}
export default UsersList
我的模态
export default function MyModal({locking}) {
return (
<div className='modal fade' id='MyModal' tabIndex='-1' aria-labelledby='MyModal' aria-hidden='true'>
...
<h5 className='modal-title' id='MyModal'>
Are you sure to block this user?
</h5>
<button type='button' className='close' data-dismiss='modal' aria-label='Close'>
<span aria-hidden='true'>×</span>
</button>
</div>
<div className='modal-footer'>
<button type='button' className='btn btn-default' data-dismiss='modal'>
<i className='fas fa-times mr-2' />
Exit
</button>
<button
type='button'
className='btn btn-success'
onClick={() => {
locking
}}
>
<i className='fas fa-check-circle mr-2' />
Confirm
</button>
</div>
</div>
</div>
</div>
)
}
解决方案
问题
- 没有点击处理程序来阻止配置文件
- 当传递
locking
给MyModal
它时立即调用(locking={locking()}
) - 在
MyModal
块按钮onClick
回调locking
未被调用(() => { locking }
解决方案
- 添加一些状态以
UserList
存储用户/配置文件 ID 以阻止/取消阻止 - 创建一个块回调以传递给模态
- 将块回调传递给
MyModal
用户列表
function UsersList({ users, userId }) {
const [profileId, setProfileId] = useState(null); // <-- create id state
function locking(pk, action) {
axios.get(`/user/${pk}/${action}/`)
.then(() => {
update();
})
.finally(() => {
setProfileId(null); // <-- clear state when done
});
}
const blockId = () => locking(profileId, 'lock'); // <-- callback to block/lock
return (
...
{users != null && users.length > 0 ? (
users.map((profile) => {
return (
<tr key={profile.id} id={profile.id} className={userId === profile.id ? 'table-active' : ''}>
...
</td>
...
{profile.id !== userId && //to be sure to not block myself
(profile.active ? (
<button
type='button'
className='btn d-block btn-danger w-5rem mb-2 badge'
data-toggle='modal'
data-target='#MyModal'
onClick={() => setProfileId(profile.id)} // <-- set id to block
>
Block
</button>
) : (
<a
className='btn d-block btn-warning w-5rem mb-2 badge'
href='#'
onClick={() => {
locking(profile.id, 'unlock')
}}
>
Unblock
</a>
))}
</td>
</tr>
)
})
) : (
)}
</tbody>
</table>
<MyModal locking={blockId} /> // <-- pass blockId callback
</div>
)
}
我的模态
export default function MyModal({locking}) {
return (
<div className='modal fade' id='MyModal' tabIndex='-1' aria-labelledby='MyModal' aria-hidden='true'>
...
<button
type='button'
className='btn btn-success'
onClick={locking} // <-- attach callback
>
<i className='fas fa-check-circle mr-2' />
Confirm
</button>
...
</div>
)
}
推荐阅读
- c# - 如何统一从uv位置获取人脸数据
- c# - 需要将帮助 C# 转换为 F#
- apache-kafka - Rootless Podman UID 映射和卷挂载问题
- javascript - react - 将 int 值映射到颜色托盘中的颜色
- javascript - 异步生成器类卡在无限循环javascript上
- python - 小部件移动上的运行方法
- laravel - 由于 $filterarray 和 $set 使用相同的字段,MongoDB updateOne 无法正常工作
- wordpress - 将旧 WordPress 网站的内容导入新的 WordPress 网站
- amazon-web-services - AWS 子网没有 IP 地址(使用 CDK)
- javascript - Jest ReferenceError:您正在尝试在 Jest 环境被拆除后“导入”文件