Javascript 如何使用三元条件使用react呈现jsx?
我想返回jsx,如果某个条件为true,如果未定义,则应返回 下面是我的代码Javascript 如何使用三元条件使用react呈现jsx?,javascript,reactjs,Javascript,Reactjs,我想返回jsx,如果某个条件为true,如果未定义,则应返回 下面是我的代码 const showInfo = (item) { return ( <div> <div> <span>name</span> </div> <div> <button>cli
const showInfo = (item) {
return (
<div>
<div>
<span>name</span>
</div>
<div>
<button>click</button>
</div>
</div>
);
}
const Parent = () => {
return (
<Child
onDone = {({item}) => {
notify ({
actions: (condition === 'value1' || condition === 'value2' ) &&
showInfo(item) //should put this condition into showInfo method
})
}}
/>
);
}
而不仅仅是行动
有人能帮我吗。谢谢。使用useState或useEffect挂钩不是更好吗?如果您想从函数返回jsx,您应该将它们包装在一些组件中。在这种情况下,您可以使用或仅使用。我看到的另一个问题是,您可能忘记了arrow函数中的arrow。也不知道变量名
条件
来自何处
const showInfo = (item) => {
return (
<>
{ condition === "value1" || condition === "value2" ? (
<div>
<div>
<span>name</span>
</div>
<div>
<button>click</button>
</div>
</div>
) : undefined}
</>
);
};
const showInfo=(项目)=>{
返回(
{条件==“值1”| |条件==“值2”(
名称
点击
):未定义}
);
};
这是否回答了您的问题?返回后showInfo函数中缺少一个左括号(
。只需使用:null
而不是:undefined
const Something: React.FC<SomethingProps> = ({
description,
actions,
...props
}) =>
(
<Header>
<Title>{title}</Title>
</Header>
{(description ||actions) && (
<Body> //this is displayed
{description && <Description>{description}</Description>}
{actions && <Actions>{actions}</Actions>}
</Body>
)}
);
{(description ||actions !== 'false') && (
<Body> //this is displayed
{description && <Description>{description}</Description>}
{actions && <Actions>{actions}</Actions>}
</Body>
)}
actions !== 'false'
const showInfo = (item) => {
return (
<>
{ condition === "value1" || condition === "value2" ? (
<div>
<div>
<span>name</span>
</div>
<div>
<button>click</button>
</div>
</div>
) : undefined}
</>
);
};