jsx:ReactJS if 条件

use*_*468 4 jsx reactjs react-native

如何在jsx:ReactJS中使用if条件?我只想如果

if user == "author" or "supervisor":
<IconButton 
   aria-label="delete" 
   onClick={() => props.pressHandler(props.id)}
 >
  <DeleteIcon style={{ color: 'red' }} />
</IconButton>
else
     no delete button
Run Code Online (Sandbox Code Playgroud)

Ash*_*rwa 5

只要把它们戴上大括号,

{ ["author", "supervisor"].includes(user) &&
<IconButton 
   aria-label="delete" 
   onClick={() => props.pressHandler(props.id)}
 >
  <DeleteIcon style={{ color: 'red' }} />
</IconButton> || null }
Run Code Online (Sandbox Code Playgroud)

参考:https://reactjs.org/docs/conditional-rendering.html#inline-if-with-logic--operator