I have a project of mine where a click should happen (click first one from collapsed list item), but it should happen automatically without user taking mouse on it(cursor) and clicking it.
that list item collapse comes from material ui.
any idea is appreciated.
my code to try : https://codesandbox.io/s/material-ui-nested-list-forked-2sqtum?file=/src/components/NestedItem/index.js
import React, { useState } from "react";
import List from "@material-ui/core/List";
import ListItem from "@material-ui/core/ListItem";
import ListItemText from "@material-ui/core/ListItemText";
import Collapse from "@material-ui/core/Collapse";
const NestedItem = ({ children }) => {
const [isOpen, setIsOpen] = useState(false);
const handleIsOpen = () => {
setIsOpen((prev) => !prev);
};
const data = ["Someshit inside Collapse", "Someshit inside Collapse 2"];
return (
<List>
<ListItem button onClick={handleIsOpen}>
<ListItemText primary={children} />
</ListItem>
<Collapse in={isOpen}>
{data.map((el) => (
<List>
<ListItem button>
<ListItemText primary={el} />
</ListItem>{" "}
</List>
))}
<List></List>
</Collapse>
</List>
);
};
export default NestedItem;
this should be clicked ('Someshit inside Collapse') when user opens the page and just once, it could print console.log('text Someshit inside Collapse is clicked'):