【发布时间】:2018-05-09 10:49:00
【问题描述】:
如何在 Context.Consumer 中使用“if”语句?我需要检查 context.state.showLoginBox 是否为真,然后显示下面的 div。
class LogInBox extends Component {
render() {
return(
<MyContext.Consumer>
{(context) => (// context.state.showLoginBox ? => show div below
<div className="logInBoxWrapper" id="logInBoxId">
<div className="logInBox">
<h3>Войти</h3>
<form method="">
<input name="email" placeholder="Электронная почта" required/>
<input name="password" type="password" placeholder="Пароль" required/>
<button type="submit">Войти</button>
</form>
</div>
</div>
)}
</MyContext.Consumer>
);
}
}
【问题讨论】:
-
把
? =>改成&&,如果你想把它当作if/else使用?:操作符
标签: reactjs react-context