React-Bootstrap 選項卡 defaultActiveKey 不隨 stateChange 改變 (React-Bootstrap Tabs defaultActiveKey is not changing with stateChange)


問題描述

React‑Bootstrap 選項卡 defaultActiveKey 不隨 stateChange 改變 (React‑Bootstrap Tabs defaultActiveKey is not changing with stateChange)

不會隨著 State 的變化而改變 defaultActiveKey

  1. 我有默認情況下打開“寫入” 一個案例場景的選項卡
  2. 然後默認打開“閱讀”;另一個案例場景的選項卡

我的代碼是這樣的

this.state = {
 defaultTab: "write"
}

functionToChangeDefaultTab()=>{
 this.setState({defaultTab: "read"})
}

render(){
 return(
   <Tabs defaultactivekey={this.state.defaultTab} transition={false} onSelect={this.onTabSelected}>
    <Tab eventKey="read" title="read"></Tab>
    <Tab eventKey="write" title="write"></Tab>
    .
    .
   </Tabs>


);
}

即使在運行 setState defaultTab 的函數後“讀取” 值,“寫”;選項卡打開。我不知道,如何動態放置defaultActiveTab。


參考解法

方法 1:

it seems your issues come from

  • wrong props ❌defaultactivekey passed to Tabs component
    ‑‑> should be camelCase: ✅defaultActiveKey
  • your function syntaxe depends on class based component. if it is the case, I reckon you wanted to do the following

  • </ul>

    class NAMEOFYOURCOMPONENT extends React.Component {
     /* ‑ This syntax ensures `this` is bound within functionToChangeDefaultTab.
        ‑ Warning: this is *experimental* syntax.
         ( from react's doc see: https://reactjs.org/docs/handling‑events.html)
        ‑ you wrote: ❌ functionToChangeDefaultTab()=>{ ... } */
    
         // ✅ should be
         functionToChangeDefaultTab = () => {
           // ... code ...
         }
    
    
         render() {
           return (
            {/* ... code ... */}
           );
         }
    }
    

    Then it should work.

    方法 2:

    why you are not using activeKey prop and passing your state

              <Tabs
              activeKey={activeTab}
              id="uncontrolled‑tab‑example"
              onSelect={(k) => {
                setactiveTab(k || '');
                nevagitation({
                  search: `?tab=${k}`,
                });
              }}
              className="bluebordertabs"
            >
         //other code
            </Tabs>
    

    (by Pankti BardoliaLaurelineusman)

    參考文件

    1. React‑Bootstrap Tabs defaultActiveKey is not changing with stateChange (CC BY‑SA 2.5/3.0/4.0)

#bootstrap-4 #javascript #tabs #reactjs #react-bootstrap






相關問題

在不同選項卡上時如何隱藏特定選項卡 (How to hide specific tab when on different tab)

如何將引導程序 4 中的內容連續居中? (How to center in bootstrap 4 the content in a row?)

構建 JavaScript 時缺少模塊 (Missing Module When Building JavaScript)

React-Bootstrap 選項卡 defaultActiveKey 不隨 stateChange 改變 (React-Bootstrap Tabs defaultActiveKey is not changing with stateChange)

表格內容的對齊方式 (alignment of the content in table)

切換器圖標無法調整導航欄寬度 (Toggler icon does not working on adjusting the navbar width)

如何根據設備大小使用屏幕 (How to use the screens based on the device size)

如何在引導程序 4 中為 col 調整大小設置動畫? (How to animate col resize in bootstrap 4?)

html頁面Bootstrap頂部的白條 (White bar at top of html page Bootstrap)

如何從 reactstrap 輸入覆蓋表單控制類? (How can I override form-control class from a reactstrap input?)

文檔似乎沒有變化,但為什麼寬度在 Bootstrap 4 和 5 之間給出不同的圖像比例? (It seems to be no change in docs, but why does width give different image scale between Bootstrap 4 and 5?)

引導表拆分單元格 (Bootstrap table split cells)







留言討論