Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
330 views
in Technique[技术] by (71.8m points)

reactjs - How to stop react material table loader whent it's need

I want to stop material table loader when custom error message shows. here is my material table component. and also i need to know how to create custom error message just like toaster when the condition is false. here is my attempt. please let me know if there is anyway to do this. because I'm totally beginner to material table.

const tableIcons = {
    Add: forwardRef((props, ref) => <AddBox {...props} ref={ref} />),
    Check: forwardRef((props, ref) => <Check {...props} ref={ref} />),
    Clear: forwardRef((props, ref) => <Clear {...props} ref={ref} />),
    Delete: forwardRef((props, ref) => <DeleteOutline {...props} ref={ref} />),
    DetailPanel: forwardRef((props, ref) => <ChevronRight {...props} ref={ref} />),
    Edit: forwardRef((props, ref) => <Edit {...props} ref={ref} />),
    Export: forwardRef((props, ref) => <SaveAlt {...props} ref={ref} />),
    Filter: forwardRef((props, ref) => <FilterList {...props} ref={ref} />),
    FirstPage: forwardRef((props, ref) => <FirstPage {...props} ref={ref} />),
    LastPage: forwardRef((props, ref) => <LastPage {...props} ref={ref} />),
    NextPage: forwardRef((props, ref) => <ChevronRight {...props} ref={ref} />),
    PreviousPage: forwardRef((props, ref) => <ChevronLeft {...props} ref={ref} />),
    ResetSearch: forwardRef((props, ref) => <Clear {...props} ref={ref} />),
    Search: forwardRef((props, ref) => <Search {...props} ref={ref} />),
    SortArrow: forwardRef((props, ref) => <ArrowDownward {...props} ref={ref} />),
    ThirdStateCheck: forwardRef((props, ref) => <Remove {...props} ref={ref} />),
    ViewColumn: forwardRef((props, ref) => <ViewColumn {...props} ref={ref} />)
  };

const Table = ({columns, data, setData, required}) => {
    const [isLoading, setIsLoading] = useState(false);
    const [tableError, setTablesError] = useState({});

    useEffect(() => {
        //showing error message
      },
      [tableError]
    );

    return (
        <MaterialTable
        columns={columns}
        data={data}
        icons={tableIcons}
        options={{
          showTitle:false,
          search:false, 
          paging:false,
          actionsColumnIndex:4,
          addRowPosition:'first',
        }}
        editable={{
          onRowAdd: newData => 
            new Promise((resolve, reject) => {
              let error = [];
              if(required.length > 0){
                var result = Object.keys(newData).map((key) => [key, newData[key]]);
                required.forEach(function(value){
                if(!result.some(row => row.includes(value))){
                    error.push(value)
                  };
                })
              }
              if(error.length > 0){
                setTablesError({error:'1', columns:error, msg:'validation'});
              }else{
                setTablesError({error:'0', columns:'', msg:''});
                setTimeout(() => {
                  setData([...data, newData]);
                  resolve();
                }, 1000)
              }
            }),
          onRowUpdate: (newData, oldData) =>
            new Promise((resolve, reject) => {
              setTimeout(() => {
                const dataUpdate = [...data];
                const index = oldData.tableData.id;
                dataUpdate[index] = newData;
                setData([...dataUpdate]);
                resolve();
              }, 1000)
            }),
          onRowDelete: oldData =>
            new Promise((resolve, reject) => {
              setTimeout(() => {
                const dataDelete = [...data];
                const index = oldData.tableData.id;
                dataDelete.splice(index, 1);
                setData([...dataDelete]);
                resolve()
              }, 1000)
            }),
        }}
      />
    )
  }

export default Table;

Thanks is advance!

question from:https://stackoverflow.com/questions/65912620/how-to-stop-react-material-table-loader-whent-its-need

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is the promise call, if you want to stop loader call resolve() function. It will resolve the promise call and loader will stop.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...