javascript - 如何将 jQuery 模态更改为 React 模态?
问题描述
我正在创建一个仅在某个参数为真时才显示的按钮。我最初是用 jQuery 和 React 编写的,但我需要将它改成只有 React。这是使用 jQuery 的代码:
currentGame() {
if (gameInfo.publicPlayersState.find(player => player.userName === userInfo.userName)) {
$(this.inCurrentGame).modal('show');
} else {
return
}
function currentGameFnctn() {
window.location.hash = `/${game._id}`;
}
return (
<React.Fragment>
<div
ref={c => {
this.inCurrentGame = c;
}}
>
<button className="ui primary button currentGame-button" onClick={this.currentGameFnctn}>
Current Game
</button>
</div>
</React.Fragment>
);
}
此代码检查一个人是否正在玩游戏,如果是,则应在他们的个人资料中显示一个按钮,允许其他人查看他们的个人资料进入该游戏。如果此人不在游戏中,则不应出现该按钮。
我将如何将其更改为 React?
解决方案
您可以使用条件渲染通过操作符来实现它&&
:
currentGame() {
function currentGameFnctn() {
window.location.hash = `/${game._id}`;
}
return (
<React.Fragment>
<div
ref={c => {
this.inCurrentGame = c;
}}
>
{player.userName === userInfo.userName &&
<button className="ui primary button currentGame-button" onClick={this.currentGameFnctn}>
Current Game
</button>
}
</div>
</React.Fragment>
);
}
推荐阅读
- html - Abosultely 位置表在页面末尾被截断
- python - 有没有办法在不使用字典的情况下计算列表中字符串的出现次数?
- r - 等效于 R 中用于 logit 回归的 SignifReg 函数?
- php - 数据库公告表 - 在其中添加可执行代码
- regex - 获取bash中两个特殊字符之间的数字
- javascript - 为什么 NodeJS MySQL 在对正确转义的 JSON 数组进行查询期间添加额外的退格键?
- r - 使用两个条件创建一个 for 循环以在 R 中分配一个变量,但在完成所有迭代后,答案没有意义
- ios - 我的异步代码执行顺序错误。这是为什么?
- c++ - 如何计算和显示正确的百分比?
- string - 重新编码字符串变量