【问题标题】:React Material UI - Tabs - How to disable lazy loading of tabs?React Material UI - 标签 - 如何禁用标签的延迟加载?
【发布时间】:2020-02-04 20:41:21
【问题描述】:

我正在根据此处显示的 Material UI 示例创建一些反应选项卡:https://material-ui.com/components/tabs/

我遇到的问题是我的选项卡延迟加载线图组件,而不是在初始页面加载时执行所有查询。有没有人对我可以在哪里进行优化以使每个选项卡在页面加载时立即加载有一些建议?

function TabPanel(props) {
  const { children, value, index} = props;
  return (
    <Typography>
      {value === index && <Box p={3}>{children}</Box>}
    </Typography>
  );
}

class SimpleTabs extends Component {
    constructor(props) {
        super(props)
        console.log(props.color)
        this.state = {
            value: 0
          };
    }

    handleChange = (event, newValue) => {
        this.setState({value: newValue});
    };

    render() {
    return (
    <div>
        <AppBar position="static">
            <Tabs value={this.state.value} onChange={this.handleChange}>
                <Tab label="Item One"/>
                <Tab label="Item Two"/>
                <Tab label="Item Three"/>
            </Tabs>
        </AppBar>
        <TabPanel value={this.state.value} index={0}>
            <LineGraph className={styles.graphBox} name={"Guest Count"} url={'http://127.0.0.1:5000/***/***'} initialFilterData={this.props.initialFilterData} filterData={this.props.filterData}/>
        </TabPanel>
        <TabPanel value={this.state.value} index={1}>
            <LineGraph className={styles.graphBox} name={"Total Sales"} url={'http://127.0.0.1:5000/***/***'} initialFilterData={this.props.initialFilterData} filterData={this.props.filterData}/>
        </TabPanel>
        <TabPanel value={this.state.value} index={2}>
            <LineGraph className={styles.graphBox} name={"Avg Check Size"} url={'http://127.0.0.1:5000/***/***'} initialFilterData={this.props.initialFilterData} filterData={this.props.filterData}/>
        </TabPanel>
    </div>
    )}
}
export default SimpleTabs;

【问题讨论】:

  • 我不确定LineGraph 组件来自哪里,但可以将静态数据集而不是url 传递给它,在这种情况下您可以使用@987654325 @方法预取所有数据集并将静态集传递给LineGraph

标签: reactjs material-ui lazy-loading


【解决方案1】:

hidden={value !== index} 添加到TabPanel 中的Typography 并移除Typography 中的value === index &amp;&amp; 条件,以便所有选项卡面板的子项立即呈现但隐藏(当前选择的除外) .

例子:

function TabPanel(props) {
  const { children, value, index, ...other } = props;

  return (
    <Typography
      component="div"
      role="tabpanel"
      hidden={value !== index}
      {...other}
    >
      <Box p={3}>{children}</Box>
    </Typography>
  );
}

【讨论】:

  • 谢谢,这有帮助。知道为什么此更改会破坏第二个和第三个标签吗?
  • 只是猜测,因为我不知道LineGraph 背后的代码是什么。我怀疑在渲染时,LineGraph 正在计算它的一些大小。由于它是隐藏的,它不占用太多空间;然后当您切换选项卡并显示它时,它不会重新计算以确定更合适的大小。
猜你喜欢
  • 2014-07-24
  • 2019-08-22
  • 1970-01-01
  • 2021-03-17
  • 1970-01-01
  • 2021-04-21
  • 1970-01-01
  • 1970-01-01
  • 2021-07-07
相关资源
最近更新 更多