I am trying to display a series of cards (components) that each carry a number that increments each time a new component is created. The problem is that instead of adding the value is concatenating and I don't really know how to solve it because I'm a bit new to react. I have tried to use valueAsNumber or parseInt() but either it doesn't let me or it doesn't work.
Here is my code:
import React, { useState } from 'react'
import {
CCol,
CRow,
CContainer,
CButton,
CCard,
CCardBody,
} from '@coreui/react'
import CIcon from '@coreui/icons-react'
import { cilArrowCircleRight } from '@coreui/icons'
import { TableCard } from '../../components'
const Tables1 = () => {
const [tableID, setTableID] = useState(1);
const [tablesList, setTablesList] = useState([]);
const handleChangeID = event => {
setTableID(event.target.value);
};
const handleClick = event => {
setTablesList([...tablesList, {tableID : tableID}]);
console.log("Before: " + event.target.value )
setTableID( event.target.value += 1 );
console.log("After: " + event.target.value)
};
return (
<>
<CContainer fluid>
<CRow className="mb-4" xs={{ cols: 1 }} sm={{ cols: 2 }} md={{ cols: 2 }} xl={{ cols: 4 }}>
{tablesList.map((id,index) => {
return (<CCol key={index} className="mb-4">
<CCard className="text-center" style={{ width: '18rem' }}>
<CCardBody>
<h2 className="card-title">{id.tableID}</h2>
<h5 className="card-title">Table</h5>
<div className="d-grid gap-2">
{/* <CButton onClick={sendLocation(value+"")}>Send <CIcon icon={cilArrowCircleRight} /> */}
<CButton onClick={() => console.log('Mesa'+id.tableID)}>Send <CIcon icon={cilArrowCircleRight} />
</CButton>
</div>
</CCardBody>
</CCard>
</CCol>)
})}
</CRow>
</CContainer>
<CButton onClick={handleClick}>Add Table</CButton>
</>
)
}
export default Tables1
And an image: