score:1
Each of your expandable panels uses the same single open
state to determine whether it should be expanded or not, that is why they all open at once. The easiest solution here is for each panel to have its own state.
Ideally, you want to extract the JSX from your map's return statement to separate component presenting a single student's panel, let's say StudentPanel
, pass the student object as prop and move the open
state and the toggle function to that component as well.
UPDATE: You'd have something like this:
class StudentPanel extends React.Component {
state = {
open: false
}
togglePanel = e => {
this.setState(prevState => ({ open: !prevState.open }))
}
render() {
const { student } = this.props;
return (
<div className = "student-container" key = {student.id}>
// (...) rest of JSX
</div>
);
}
}
In render
of your App
you'd have:
(...).map(student => <StudentPanel student={student} />)
and the open
property and togglePanel
can be removed from App
component completely.
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
- drop down opens up on every Item I map through How can I make open up only on the one I clicked
- How to make one table record editable on button click in React when the table record is mapped?
- 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 parent component pass prop to children only when button is clicked
- How to make only one API request when using React SSR?
- how to revert (last clicked list item) back to its original color when any other item is clicked - react hooks
- 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 can I check only one check box at a time instead of all in a list of checkboxes in React
- How to expand and collapse the table rows in plain react js. Show only one row if the description of the row is multiple and show the expand button
- How can I make my React app only render when an expensive calculation is finished?
- How can I make multiple styling for only one prop in React Styled Components?
- How can I stop the Select component from closing when I clicked one of its item(Input element) - react antd
- How can I change the state of clicked button only in react hooks
- How can we make sure other <ListItem> components as collapsible when one <ListItem> component get expands under <List> react material ui component?
- How can i make it so that data sends only one item in array to cart
- How can i make all elements dissapear when one element is shown in react js?
- How can I pass id/class of a button present in a handlebar file into the typescript to launch a react modal when button is clicked
- 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
- How I can check the same conditon when Button is clicked in React JS?
- React JS how to remember the clicked item in a list when I pass to next page
- How Can I make my React Button Component updates CSS only on the specific page?
- How to click a button into a list array items only effect the related item using React hooks
- When I click the delete button the top item on the list deletes. How can I fix it?
- How can I rerender only one item in a flatlist?
- How to keep only one radio button selected with React
- How do I get acces to only one item from map method in react js
More Query from same tag
- Editing a record: conflict between props and state
- React-Redux-Forms enable checkbox in another react component
- Trigger React Pikaday calendar from an icon onClick
- Correct way to update a state array in a Flux Store
- Why are JS / React much quicker than WebAssembly / Rust?
- How can I access an object in a complex JSON file as follows?
- Button next to Typography
- Expected number of calls: >= 1 Received number of calls: 0
- How i can get suspense to work on react 18?
- How do you override the css for toggle buttons from react bootstrap?
- What is the best way to use a list as a key in JavaScript?
- How can I make use of REST with <Resource ...> for a custom page
- react router on how to collapse the navbar on mobile when selecting the menu links
- redux-form: need help in understanding syntax
- React.js - how to create Accordeon (talking to siblings)
- material ui lab date picker, Can't resolve '@material-ui/lab/AdapterDateFns'
- How to I fill a nested array with setState?
- React: duplicate values only display on first item in material ui table
- Handle Change the object in formik. using setFieldValue
- How to add elements in cytoscape graph from react component?
- React-bootstrap onSelect method to change state
- Adding locale.js in moment React
- How to emulate this jQuery behaviour with reactJS: add/ remove className from different divs
- TypeScript/React: Defining state outside or inside a class
- ReactJS: Where in the project to put a Javascript function that is called when a onclick() event occurs
- React Component and constructor bind not working
- Using Material-UI KeyboardTimePicker, how can you change the time without changing the date?
- create multiple react checkboxes from JS object
- How to use zustand to store the result of a query
- My React Component is rendering twice because of Strict Mode