【问题标题】:How to reset material ui tabs when url changes?url更改时如何重置材质ui选项卡?
【发布时间】:2017-10-23 05:01:53
【问题描述】:

我在子组件中使用 material-ui 选项卡,每次 URL 更改时都需要重新呈现选项卡。 url 结构类似于:

/in/:id

我的简化代码如下所示(如果示例中没有包含 var,请忽略):

class Header extends Component {

  constructor(props){
     super(props);
     this.state = {
     selectedTab: 0
    }
   }
   componentWillReceiveProps(nextProps){
     this.setState({selectedTab: 0});
   }
   render(){

     return (
      <Tabs
       tabItemContainerStyle={style.tabItem}
       className="active-tab-bar tabs"
       initialSelectedIndex={this.state.selectedTab}
      >
      <Tab
        containerElement={<Link to={`/in/${currentClient.id}/timeline`}/>}
        to={`/in/${currentClient.id}/timeline`}
        icon={<FontIcon className="material-icons">playlist_add_check</FontIcon>} />
      <Tab
       containerElement={<Link to=
       {`/in/${currentClient.id}/website`}/>}
       to={`/in/${currentClient.id}/website`}
       icon={<FontIcon className="material-icons">web</FontIcon>} />
      </Tabs>
     )

}
export default withRouter(connect(null, {selectClient})(Header));

我尝试使用 connectWillReceiveProps 将所选索引重置为 0,但选项卡会在 url 更改后保持选择。有什么想法如何在每次 url 更改时重置标签?

【问题讨论】:

    标签: reactjs react-redux material-ui


    【解决方案1】:

    您需要将您的 Tabs 转换为受控组件。

    render(){
    
         return (
          <Tabs
           value={this.state.selectedTab}
           onChange={(value)=>this.setState({selectedTab:value})}
           tabItemContainerStyle={style.tabItem}
           className="active-tab-bar tabs"
           initialSelectedIndex={this.state.selectedTab}
          >
          <Tab value=0
            containerElement={<Link to={`/in/${currentClient.id}/timeline`}/>}
            to={`/in/${currentClient.id}/timeline`}
            icon={<FontIcon className="material-icons">playlist_add_check</FontIcon>} />
          <Tab value=1
           containerElement={<Link to=
           {`/in/${currentClient.id}/website`}/>}
           to={`/in/${currentClient.id}/website`}
           icon={<FontIcon className="material-icons">web</FontIcon>} />
          </Tabs>
         )
    
    }
    

    【讨论】:

    • 添加 value={this.state.selectedTab} 后,选项卡可以工作,但所选选项卡下的下划线根本不显示。
    • 您可以使用 Tab 的onActive 事件来设置所选元素的样式。或者您可以更改 this.state.selectedTab 以存储选项卡的 Link 并将其分配给选项卡的 value 属性。希望有效!
    • 看看这个implementation。我已将 React Router v4 与 material-ui 1 一起使用。选项卡按预期工作。如果需要,提出问题。
    • 没有添加另一个答案,因为实现在这个答案的行中。
    【解决方案2】:

    看起来选项卡道具仅用于初始: “initialSelectedIndex”,您需要将活动选项卡带到您的控件中。 将 initialSelectedIndex 替换为:

    value={this.state.value}
    onChange={this.handleChange}
    

    并添加:

      constructor(props) {
        super(props);
        this.state = {
          value: 'a',
        };
      }
    
      handleChange = (value) => {
        this.setState({
          value: value,
        });
      };
    

    现在您可以在 willreciveprops 中的 url 更改时更改活动选项卡...

    完整示例:(来自:http://www.material-ui.com/#/components/tabs

    import React from 'react';
    import {Tabs, Tab} from 'material-ui/Tabs';`enter code here`
    
    const styles = {
      headline: {
        fontSize: 24,
        paddingTop: 16,
        marginBottom: 12,
        fontWeight: 400,
      },
    };
    
    export default class TabsExampleControlled extends React.Component {
    
      constructor(props) {
        super(props);
        this.state = {
          value: 'a',
        };
      }
    
      handleChange = (value) => {
        this.setState({
          value: value,
        });
      };
    
      render() {
        return (
          <Tabs
            value={this.state.value}
            onChange={this.handleChange}
          >
            <Tab label="Tab A" value="a">
              <div>
                <h2 style={styles.headline}>Controllable Tab A</h2>
                <p>
                  Tabs are also controllable if you want to programmatically pass them their values.
                  This allows for more functionality in Tabs such as not
                  having any Tab selected or assigning them different values.
                </p>
              </div>
            </Tab>
            <Tab label="Tab B" value="b">
              <div>
                <h2 style={styles.headline}>Controllable Tab B</h2>
                <p>
                  This is another example of a controllable tab. Remember, if you
                  use controllable Tabs, you need to give all of your tabs values or else
                  you wont be able to select them.
                </p>
              </div>
            </Tab>
          </Tabs>
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-06-10
      • 2017-02-05
      • 2018-06-04
      • 2019-04-27
      • 1970-01-01
      • 2016-05-09
      • 1970-01-01
      • 2019-08-25
      • 1970-01-01
      相关资源
      最近更新 更多