我处于react的学习阶段,我定义了一个带有三元运算符条件的,内联样式的对象,但它根本无法正常工作!有人可以帮我如何实现这一目标吗
import React from 'react';
function Joke(props)
{
const styles = {
fontSize: "30px",
display: props.question && props.answer ? "block" : "none",
display: props.punchline ? "block" : "none",
color: !props.question && "#888888"
}
return(
<div>
<p className="qus" style={styles}> {props.jokes.question} </p>
<p className="ans" style={styles}> {props.jokes.answer} </p>
<p className="punch" style={styles}> {props.jokes.punchline} </p>
</div>
)
}
export default Joke
/*--------------------------------------------------------------------*/
import React from 'react';
import Joke from "./Joke"
function App()
{
return (
<div>
<Joke jokes={{
question:"who is the president",
answer:"Trump"}} />
<Joke jokes={{
punchline: "hey look there is no question and answer here but there is no space been taken by those"}} />
<Joke jokes={{
question:"what is 1+1",
answer:"11"}}/>
</div>
)
}
export default App
在某些情况下,我正在发送带有问题和答案的对象,在某些情况下,我只是在发送紧急通知而不是提问和答案,有人可以告诉我是否存在一种在对象内部设置三元条件的方法,每当我将props对象作为参数传递给Joke函数时,都会被应用
慕斯王
相关分类