无法执行if语句

时间:2019-12-05 16:22:05

标签: javascript reactjs material-ui

我觉得这可能是一个简单的解决方案,但是我很难理解为什么我无法摆脱有关if语句的错误。

关于if语句,我遇到许多语法错误 if (selectedDate < new Date().toISOString().split('T')[0]) { selectableRows: "none" }

我尝试添加一个返回值(selectableRows:“无”),但这也无法解决我的问题。

 const custom = {
        customToolbar: () => <DataTableRefreshButton refresh={refresh} />,
        customToolbarSelect: (selectedRows, displayData) => (
            <TestToolBar
                data={data}
                alert={alert}
                rows={rows}
                type={type}
            />
        ),
        isRowSelectable: (displayData) => {

            const data= updatedStaff[
                data
            ];
         return (currentData.ActionTypeID > 0 || currentData.ActionCompletedByID > 0   ? false : true)
        },
         if (selectedDate < new Date().toISOString().split('T')[0]) {
        selectableRows: "none"
        }
    };

1 个答案:

答案 0 :(得分:1)

简化,您的结构如下:

const custom = {
    firstProperty: 1,
    secondProperty: 'test',
    if (someCondition) {
        thirdProperty: 'third'
    }
};

这是无效的JavaScript。编写多个语句时可以使用if块,但不能在设置对象属性的过程中使用。

如果该属性有一个有效的默认值,则可以使用三元条件运算符进行设置:

const custom = {
    customToolbar: ...
    customToolbarSelect: ...
    isRowSelectable: ...
    selectableRows: (selectedDate < new Date().toISOString().split('T')[0]) ? 'none' : 'default'
}

否则,您可能需要更动态地构建对象。像这样:

const custom =
    (selectedDate < new Date().toISOString().split('T')[0])
    ? { /* properties with selectableRows */ }
    : { /* properties without selectableRows */ }