score:1
Accepted answer
You set open
for all items in list. You must narrow it to the currently selected student ids. So you need an array that will hold all opened ids.
const [open, setOpen] = useState([])
const toggleOpen= (id) => {
if (open.includes(id)) {
setOpen(open.filter(sid => sid !== id))
} else {
let newOpen = [...open]
newOpen.push(id)
setOpen(newOpen)
}
}
<button className="expand-btn" onClick={() => toggleOpen(student.id)}>{open.includes(student.id) ? '-' : '+'}</button>
and then
{open.includes(student.id) ? (
<ul className="grades-list">
{student.grades.map((grade, index) => <li key={grade.id}>Test {index + 1}: {grade}%</li>)}
</ul>) : null}
Source: stackoverflow.com
Related Query
- How can I make only one list item expand when a button is clicked in React (no hooks)?
- how to make only one list item expand when clicked- react hooks
- how to revert (last clicked list item) back to its original color when any other item is clicked - react hooks
- How to stop re-rendering a whole list of items when only one item of the list is created or updated in ReactJs React-Redux?
- How to make only one API request when using React SSR?
- how to change the style of only one react component that is being render through .map() when the component is clicked
- make only the clicked item on a list active in react
- How do I make my TableData react component reusable onClick when its only posting one under my table?
- Toggle dropdown list of only one div from list of multiple divs when clicked in react
- drop down opens up on every Item I map through How can I make open up only on the one I clicked
- React JS how to remember the clicked item in a list when I pass to next page
- How to click a button into a list array items only effect the related item using React hooks
- How to do a react hooks api call and display only one item
- Need to make navlinks to show only when bar is clicked on smaller screened devices in react hooks
- How do I get acces to only one item from map method in react js
- Change items in list in React when an item has been clicked
- How to target single item in list with onClick when mapping JSON array in React
- React re-renders entire list when one item is changed
- How to make one table record editable on button click in React when the table record is mapped?
- how to make parent component pass prop to children only when button is clicked
- How to re-render only one component on hover when state changes in React
- how to handle click only one <li> item in react
- How to make a tab active when it is clicked on in React
- React Hooks and React Query (useQuery): invoking the API call/ query only when a button is clicked
- How to expand more/less only one ListItem using single method in react
- How to mitigate useless React renders when large state is present but only one changes?
- How to show only one active item at a time with react and material ui
- How can I check only one check box at a time instead of all in a list of checkboxes in React
- Implement shouldComponentUpdate with React hooks - Render child only once, but make DOM changes when props change
- How to refresh the table data when one of the item has its valid till date and time has expired in React Js
More Query from same tag
- Optional chaining issues when running test with jest
- i cannot make button sort by json in react
- React unable to update props
- Issue laying out components with React and Flexbox
- Facing ant-design form validation issue on production
- How do I use Component props in actionCreators.js?
- Component renders and gives an error before the data is completely loaded from the API
- react-countdown-now- prevent re-render when calling setState on parent component
- How to have scrollbar start at a certain position on page load in React
- React performance in mobile browser
- How to access react-router-v4 history prop from MobX store
- how to set current location getting from goelocation into google map using react
- Trouble Recovering Data From API with Async in React
- Show popup for only one Marker react map gl
- undefined React JS variable
- Changing Bootstrap's responsiveness
- How to upload image to an Amazon S3 bucket using React js?
- React: updating array props won't trigger useEffect hook
- Webpack file-loader publicPath
- Disabling the debugging tab in the iOS simulator
- CSS & React / Transition to change background color and font color isn't working
- Google analytics with react is not working
- Why can I omit the return statement with parenthesis in React
- react router routing with parameter id
- Typescript library import with sub paths
- Loading when fetching firestore data
- (React) How to get the state of another component when a submit button is clicked?
- Firebase Storage Web: uploadTask.cancel() is not working reactjs
- How to create button with function of draw polygons in Leaflet?
- React router Redirect does not work when route comes from another component