If / Else在Payload JSX中

时间:2018-02-08 20:34:16

标签: javascript reactjs jsx

我是新手做出反应和jsx ...有关我的有效负载语法有什么问题吗?如果typeinternal我想发送media_file,或typeexternal,我想发送url

const createMedia = (payload) => {
const data = excludeObjNullValue({
    name: payload.name,
    content_id: payload.contentId,
    destination_id: payload.destinationId,
    task_id: payload.taskId,
    type: payload.type,
    if(type === 'internal') {
        media_file: payload.mediaFile;
    } else {
        url: payload.url;
    }
}, true);
const formData = new FormData();
for (const [k, v] of Object.entries(data)) {
    formData.append(k, v);
}
return request.postFormData('/media', formData);
};

1 个答案:

答案 0 :(得分:3)

您无法在对象定义中使用if/else。您应首先创建一个对象,然后使用if/else向该属性添加属性。

let obj = {
    name: payload.name,
    content_id: payload.contentId,
    destination_id: payload.destinationId,
    task_id: payload.taskId,
    type: payload.type
}
if(type === 'internal') {
   obj.media_file = payload.mediaFile;
} else {
   obj.url = payload.url;
}
const data = excludeObjNullValue(obj, true);