I am working with react hooks, And encounter a problem where I have been stuck now.
I have one json data which I am getting whenever user logs in
so by that I am creating one top bar which shows user names
and when user click on those names I am showing some data which they have created or present for them in db, That data I am already getting in data
MY data I am getting from server
let data= [
{
"id": 1,
"name": "Maxi",
"myData": [
{
"data_name": "div1",
"data_title": "div1 tittle"
},
{
"data_name": "div1",
"data_title": "div tittle"
}
]
},
{
"id": 2,
"name": "Phill",
"myData": [
{
"data_name": "div21",
"data_title": "div21 tittle"
}
]
}
]
I have one button on side on which I want to create new div, inside the name I have selected from top.
SO suppose I have listed the topbar
and there are total of 3 data i.e 3 names, so if data length is smaller than 4 what I am doing, showing a plus button to create one more tab, so user can create one more tab and then they can create one div inside there each tab,
What I have done
I am populating the top bar as well as when I click on side button I am creating one div
inside that selected tab.
Issues I am facing
- if length of data is less then four I am providing a plus button to create on more tab (give it default name), but I am not getting any idea how can I create the new one.
- when I click on side button to create one dive inside tab, and I go to other tab and come back again I want that created
div
should not gets deleted. - Suppose I have 3 tabs (data) so when page loads the first tab data I want to show, but it is not showing up I need to click there only it is showing up.
- if the data is null then I can create up to 4 tabs this also I a=want some Idea to do it.
I am linking my code sand box here so it will be easy to understand what I have done and what I am trying to do
Here in code sand box I have written all my code please check it out.
I have explained every thing what I a m trying to do, I know how to code but not getting any idea or approach.
if there is no data for user then I will start creating tabs and nested elements from 1 for that user.
in each there will be a save button for each new element created, so I will do that, my only consern is to not to loss data on ui when I go one tab to other once I created them
export default function App() {
const [active_menu, setactive_menu] = useState(0);
const [nestedData, setnestedData] = useState(null);
let data1 = [
{
id: 1,
name: "Maxi",
myData: [
{
data_name: "div1",
data_title: "div1 tittle"
},
{
data_name: "div1",
data_title: "div tittle"
}
]
},
{
id: 2,
name: "Phill",
myData: [
{
data_name: "div21",
data_title: "div21 tittle"
}
]
}
];
const Tab_click = (index, li) => {
setactive_menu(index);
setnestedData(li);
};
const Create_element = () => {
//here I don't know how to create new elements
};
const addnewTab=()=>{
}
return (
<div className="App row">
{data1.map((li, index) => (
<div className="col-4 col-sm-4 col-md-3 col-lg-3 col-xl-3" key={index}>
<div
className={
index === active_menu
? "row dashboard_single_cont_active"
: "row dashboard_single_cont"
}
onClick={() => Tab_click(index, li)}
>
<div className="dashboard_name col-10 col-sm-10 col-md-9 col-lg-10 col-xl-10">
{li.name}
</div>
<div
className={
active_menu === index
? "dashboard_option_active col-2 col-sm-2 col-md-3 col-lg-2 col-xl-2"
: "dashboard_option col-2 col-sm-2 col-md-3 col-lg-2 col-xl-2"
}
align="center"
></div>
</div>
</div>
))}
{data1.length < 4 && (
<span
onClick={addnewTab}
>ADD</span>
)}
<div className="col-11 col-sm-11 col-md-11 col-lg-11 col-xl-11">
<div className="row">
{nestedData !== null &&
nestedData.myData.map((li, index) => (
<div className="col-11 col-sm-11 col-md-8 col-lg-6 col-xl-6">
<NEsted data={li} />
<br></br>
</div>
))}
</div>
</div>
<RightBar Create_element={Create_element} />
</div>
);
}
See Question&Answers more detail:os