@@ -23,7 +23,7 @@ React Dynamic Tabs with full API
2323
2424- [ Installation] ( #installation )
2525- [ Basic Example] ( #basic-example )
26- - [ ready function ] ( #ready-function )
26+ - [ Simple manipulation ] ( #simple-manipulation )
2727- [ Options] ( #options )
2828 - [ tabs] ( #tabs )
2929 - [ selectedTabID] ( #selectedTabID )
@@ -82,63 +82,88 @@ $ yarn add react-dyn-tabs
8282## Basic Example
8383
8484``` js
85- import React , {useEffect } from ' react' ;
86- import useDynTabs from ' react-dyn-tabs' ;
85+ import React from ' react' ;
8786import ' react-dyn-tabs/style/react-dyn-tabs.css' ;
8887import ' react-dyn-tabs/themes/react-dyn-tabs-card.css' ;
89- import ContactComponent from ' ./contact-component ' ;
88+ import useDynTabs from ' react-dyn-tabs ' ;
9089
9190export default () => {
9291 const options = {
9392 tabs: [
9493 {
9594 id: ' 1' ,
96- title: ' home' ,
97- closable: false ,
98- panelComponent : (porps ) => < p> home content < / p> ,
95+ title: ' tab 1' ,
96+ panelComponent : (porps ) => < p> panel 1 < / p> ,
9997 },
10098 {
10199 id: ' 2' ,
102- title: ' contact ' ,
103- panelComponent: ContactComponent, // or can be <ContactComponent />
100+ title: ' tab 2 ' ,
101+ panelComponent : ( porps ) => < p > panel 2 < / p > ,
104102 },
105103 ],
106104 selectedTabID: ' 1' ,
107- onLoad : function () {},
108105 };
109- const [TabList , PanelList , ready ] = useDynTabs (options);
110- useEffect (() => {
111- ready ((instance ) => {
112- // open more tabs
113- instance .open ({id: ' 3' , title: ' Tab 3' , panelComponent : (porps ) => < p> Tab 3 content < / p> });
114- instance .open ({id: ' 4' , title: ' Tab 4' , panelComponent : (porps ) => < p> Tab 4 content < / p> });
115- // switch to new tab
116- instance .select (' 4' );
117- });
118- }, []);
106+ const [TabList , PanelList ] = useDynTabs (options);
119107 return (
120- < div >
108+ <>
121109 < TabList>< / TabList>
122110 < PanelList>< / PanelList>
123- < / div >
111+ < / >
124112 );
125113};
126114```
127115
128- ## ready function
116+ ## Simple manipulation
129117
130- - ready function is returned by useDynTabs hook.
118+ ``` js
119+ import React from ' react' ;
120+ import ' react-dyn-tabs/style/scss/react-dyn-tabs.scss' ;
121+ import ' react-dyn-tabs/themes/scss/react-dyn-tabs-card.scss' ;
122+ import useDynTabs from ' react-dyn-tabs' ;
123+
124+ export default () => {
125+ const options = {
126+ tabs: [
127+ {
128+ id: ' 1' ,
129+ title: ' tab1' ,
130+ panelComponent : (porps ) => < p> panel 1 < / p> ,
131+ },
132+ {
133+ id: ' 2' ,
134+ title: ' tab2' ,
135+ panelComponent : (porps ) => < p> panel 2 < / p> ,
136+ },
137+ ],
138+ selectedTabID: ' 1' ,
139+ };
140+ let _instance;
141+ const [TabList , PanelList , ready ] = useDynTabs (options);
142+ ready ((instance ) => {
143+ _instance = instance;
144+ });
145+ const addTab3 = function () {
146+ // open tab 3
147+ _instance .open ({id: ' 3' , title: ' Tab 3' , panelComponent : (porps ) => < p> panel 3 < / p> });
148+ // switch to tab 3
149+ _instance .select (' 3' );
150+ };
151+ return (
152+ <>
153+ < button onClick= {addTab3}> Add tab 3 < / button>
154+ < TabList>< / TabList>
155+ < PanelList>< / PanelList>
156+ < / >
157+ );
158+ };
159+ ```
131160
132- - ready function accepts a function as a parameter and calls it with instance object after the first render, when the component is mounted.
161+ ** NOTE : **
133162
134163- Tabs can't be manipulated safely before the first render, use ready() to make a function available after the component is mounted.
135164
136- - ready function can be called multiple times
137-
138165- ready function and instance Object will not be changed after re-rendering multiple times.
139166
140- - When ready function is called after the first render, it calls its function parameter with instance object immediately.
141-
142167## Options
143168
144169### tabs
@@ -426,6 +451,10 @@ const [TabList, PanelList, ready] = useDynTabs({
426451});
427452```
428453
454+ ** NOTE :**
455+
456+ Do not use setState inside the onInit callback because it leads to an infinite loop.
457+
429458### onChange
430459
431460<table >
@@ -636,7 +665,7 @@ Parameters:
636665** Example**
637666
638667``` js
639- const result = instance .isOpen (' tab ID' );
668+ const result = instance .isOpen (' Your tab ID' );
640669```
641670
642671### open
@@ -680,7 +709,7 @@ Parameters:
680709** Example**
681710
682711``` js
683- const result = instance .isSelected (' tab ID' );
712+ const result = instance .isSelected (' Your tab ID' );
684713```
685714
686715### select
@@ -715,8 +744,6 @@ Closing an already closed tab only triggers 'onInit' event.
715744
716745It switches to the previously selected tab before closing if switching parameter was true and tab was already opened and selected.
717746
718- When the user clicks on the default close icon, close function is called with true value as a switching parameter.
719-
720747Return value : Promise
721748
722749Parameters:
@@ -745,9 +772,7 @@ Return value : Promise
745772** Example**
746773
747774``` js
748- instance .refresh ().then (({currentData, instance}) => {
749- // do sth here
750- });
775+ instance .refresh ().then (({currentData, instance}) => {});
751776```
752777
753778### getOption
@@ -945,7 +970,7 @@ const {selectedTabID, openTabIDs} = instance.getPreviousData();
945970 <tr>
946971 <td>panelComponent</td>
947972 <td>React Element | React Component | null</td>
948- <td>function component which returns empty div</td>
973+ <td>A function component which returns empty div</td>
949974 <td>false</td>
950975 </tr>
951976 <tr>
0 commit comments