I have this RenamePopover component with an input field, which I'm using inside a map function located in the Board componenent. As I loop over the array, I'm passing "board.name" into the RenamePopover as a value prop, so that in the end, every rendered element would have its own popover with its input field prepopulated with the name. Unfortunately, that's not the case, since after rendering every input field is set to the name of the last element in the array. What I am missing?
Board.js
import React from 'react'
import { useEffect } from 'react';
import { useSelector, useDispatch } from 'react-redux';
import { getActiveBoard } from '../../state/action-creators/activeBoardActions';
import RenamePopover from '../popovers/RenamePopover';
const Board = () => {
const dispatch = useDispatch();
const [anchorEl, setAnchorEl] = React.useState(null);
const boards = useSelector((state) => state.board.items);
const open = Boolean(anchorEl);
useEffect(() => {
dispatch(getActiveBoard());
}, [boards])
const handleClick = (id) => {
const anchor = document.getElementById(`board-anchor${id}`);
setAnchorEl(anchor);
};
const handleClose = () => {
setAnchorEl(null);
};
const single_board = boards && boards.map((board) => {
return (
<li key={board.id} className={`row hovered-nav-item board-item ${active_board == board.id ? "item-selected" : ""}`}>
<span className="d-flex justify-content-between">
<div onClick={() => onBoardClick(board.id)} className="fs-5 text-white board-name" id={`board-anchor${board.id}`}>
{board.name}
</div>
<svg onClick={() => handleClick(board.id)} xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor"
className="bi bi-pen-fill rename-add-icon rename-add-icon-sidebar"
viewBox="0 0 16 16">
<path
d="m13.498.795.149-.149a1.207 1.207 0 1 1 1.707 1.708l-.149.148a1.5 1.5 0 0 1-.059 2.059L4.854 14.854a.5.5 0 0 1-.233.131l-4 1a.5.5 0 0 1-.606-.606l1-4a.5.5 0 0 1 .131-.232l9.642-9.642a.5.5 0 0 0-.642.056L6.854 4.854a.5.5 0 1 1-.708-.708L9.44.854A1.5 1.5 0 0 1 11.5.796a1.5 1.5 0 0 1 1.998-.001z" />
</svg>
<RenamePopover
open={open}
anchorEl={anchorEl}
onClose={handleClose}
placeholder="Enter new name"
value={board.name}
/>
</span>
</li>
)
})
return(
<div className="container" id="sidebar-boards">
{single_board}
</div>
)
}
export default Board
RenamePopover.js
import { Popover } from '@material-ui/core'
import React from 'react'
const RenamePopover = (props) => {
const [value, setValue] = React.useState(props.value);
const handleChange = (e) => {
setValue(e.target.value);
}
return (
<Popover
open={props.open}
anchorEl={props.anchorEl}
onClose={props.onClose}
anchorOrigin={{
vertical: 'center',
horizontal: 'right',
}}
transformOrigin={{
vertical: 'center',
horizontal: 'left',
}}
>
<input autoFocus={true} className="card bg-dark text-light add-input" type="text"
placeholder={props.placeholder}
value={value}
onChange={handleChange}
/>
</Popover>
)
}
export default RenamePopove
r