3 回答
TA贡献1848条经验 获得超10个赞
您可以将单元格内容存储在变量中:
let content;
if(email) {
content = <span>...</span>;
} else if(icon) {
content = <span>...</span>;
} else {
content = <span>...</span>;
}
return <td>{content}</td>;
TA贡献1807条经验 获得超9个赞
另一种选择是使用枚举之类的东西来呈现:
if (email) {
content = 'email';
else if (icon) {
content = 'icon';
} else {
content = 'other';
}
return (
<td>
{{
email: <span>...</span>,
icon: <span>...</span>,
other: <span>...</span>,
}[content]}
</td>);
这在这里更详细地解释:https ://reactpatterns.js.org/docs/conditional-rendering-with-enum/
TA贡献1873条经验 获得超9个赞
我发现提取复杂功能以提高可读性很有用:
import React from 'react';
// extracted functionality
const emailAction = (email, icon) => {
if (email) {
return <span>Do This</span>;
} else {
if (icon) {
return <span>Do That</span>;
} else {
return <span>Do Something Else</span>;
}
}
};
// your Component
export const TableData = (props) => {
return <td>{emailAction(props.email, props.icon)}</td>;
};
添加回答
举报