【问题标题】:Showing Circular indeterminate Progress on button click单击按钮时显示圆形不确定进度
【发布时间】:2021-11-08 16:48:28
【问题描述】:

我有一个按钮,单击该按钮会运行一些从另一个网站导入数据的代码。最好有一个循环不确定指示器(材料 UI),它显示导入正在进行中,直到完成。

下面的代码显示了我正在运行的代码和一些 jsx。

<Box mt={5} mb={5} width={1200}>
            <Grid
              container
              justify="center"
              spacing={2}
              style={{ padding: '25px' }}
            >
              <Grid item xs={7}>
                <Typography variant="h3" style={{ fontWeight: 600 }}>
                 List
                </Typography>
                <Typography variant="h5" style={{ marginTop: '2em' }}>
                  some text
                </Typography>
                <Button
                  variant="contained"
                  color="primary"
                  size="large"
                  style={{ marginTop: '1em' }}
                  // style={{ maxWidth: '108px', minWidth: '108px' }}
                  onClick={() => {
                    let arrCollection = [];
                    const stream = fetch(
                      'https://othersite.org/api/games/user/neio',
                      { headers: { Accept: 'application/x-ndjson' } }
                    );

                    const onMessage = obj => {
                      arrCollection.push(obj);
                    };

                    const onComplete = () =>
                      console.log('The stream has completed');

                    stream.then(readStream(onMessage)).then(onComplete);

                    console.log('arrCollection', arrCollection);
                  }}
                >
                  Import Games
                </Button>
              </Grid>
            </Grid>
          </Box>

我想不通的是如何在导入时显示“循环不确定”然后显示完成。

这是循环不确定代码的链接:https://material-ui.com/components/progress/

我需要在 Dom 中添加一个节点吗?

【问题讨论】:

  • 不能只使用状态变量吗?当 onClick 函数开始时将其设置为 true,当完成时将其设置为 false。然后根据该变量切换您的 UI。
  • 你是对的。谢谢。

标签: reactjs material-ui


【解决方案1】:

如前所述,您可以使用状态来管理它。 像这样的东西应该可以工作

const [loading, setLoading] = useState(false);

  <div>{loading ? <LoadingIndicator /> : ''}</div>
  <Button
    onClick={() => {
      setLoading(true);

      let arrCollection = [];
      const stream = fetch('https://othersite.org/api/games/user/neio', {
        headers: { Accept: 'application/x-ndjson' },
      });

      const onMessage = (obj) => {
        arrCollection.push(obj);
      };

      const onComplete = () => console.log('The stream has completed');

      stream.then(readStream(onMessage)).then(onComplete);

      console.log('arrCollection', arrCollection);

      setLoading(false);
    }}
  >
    Import Games
  </Button>

【讨论】:

  • 我现在已经解决了,但是您的回答是我所做的事情的类型,谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-27
  • 2023-03-23
  • 2022-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多