Hello Guys This is how my code looks like
export default function Billing() {
const classes = useStyles();
const [balance, setBalance] = useState('0.00');
const [upcoming, setUpcoming] = useState('0.00');
const [lastmonth, setLastmonth] = useState('0.00');
const [header, setHeader] = useState('Please Enter The Amount');
const [open, setOpen] = useState(false);
const [amountstate, setAmountstate] = useState(false);
const [amounthelper, setAmounthelper] = useState('');
const [sairam, setSairam] = useState(0);
const onchange = async (e) => {
console.log(sairam)
setSairam({amount: e.target.value})
}
const [modalchild, setModalchild] = useState(<>
<form noValidate autoComplete="off">
<TextField
error={amountstate}
type="number"
value={sairam}
onChange={onchange}
label='Please enter the amount'
helperText={amounthelper}
variant="outlined"
/>
<br />
<br />
<Button variant="contained" color="primary" onClick={() => addBalance()}>Add Balance</Button>
</form>
</>);
const [country, setCountry] = useState(false);
const [currency, setCurrency] = useState('');
const addBalance = () => {
console.log("Clicked :)")
console.log(sairam) // outputs the inital value not the changed value
});
return(<>
<div className="balance-container">
<div className="columns-top">
<div className="column-top">
<h1>${upcoming}</h1>
</div>
<div className="column-top">
<h1>${balance}</h1>
<Button variant="contained" color="primary" onClick={handleOpen}>Add Balance</Button>
</div>
<div className="column-top">
<h1>${lastmonth}</h1>
</div>
</div>
</div>
<Modal
aria-labelledby="transition-modal-title"
aria-describedby="transition-modal-description"
className={classes.modal}
open={open}
onClose={handleClose}
closeAfterTransition
BackdropComponent={Backdrop}
BackdropProps={{
timeout: 500,
}}
>
<Fade in={open}>
<div className={classes.paper}>
<h2 id="transition-modal-title">{header}</h2>
{modalchild}
</div>
</Fade>
</Modal>
</>
)
}
if I change the state sairam by using the function setSairam() the state is not getting updated and its only logging the initial state which is 0 i have even tried the loggint the event value of my input it works correct but the state alone is not changing please help me feel free to give me any other options
question from:
https://stackoverflow.com/questions/65560158/the-usestate-hook-is-not-working-in-react 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…