I'm trying to use a fetched value from DB as an initial value (shown until the component is clicked) of an input field.
However, when clicking on or typing in the input field, value of the input field persists meaning that a new value cannot be entered.
Component's file:
import React, { useEffect, useState } from "react";
import InputItem from './InputItem';
import Grid from '@material-ui/core/Grid';
import axios from 'axios';
interface Props {
income: string;
setIncome: (value: string) => void;
totalExpenses: number;
currencySymbol: string;
}
const IncomeExpensesContainer: React.FC<Props> = ({
income,
setIncome,
totalExpenses,
currencySymbol,
}: Props) => {
// Initializing didMount as false
const [didMount, setDidMount] = useState(false);
// Setting didMount to true upon mounting
useEffect(() => { setDidMount(true) }, []);
useEffect(() => {
if (didMount) {
axios.delete('http://localhost:4000/app/income')
.catch(function (error) {
console.log(error);
});
axios.post('http://localhost:4000/app/income',
{
income: income
});
}
}, [didMount,income]);
return (
<Grid container spacing={1} className="income-expenses-container">
<Grid item xs={6}>
<InputItem
onBlur={setIncome}
title="Income"
type="number"
placeholder="Your income"
value={income}
/>
</Grid>
<Grid item xs={6} className="centered">
<h3>Total Expenses</h3>
<div>{totalExpenses}{currencySymbol}</div>
</Grid>
</Grid>
);
}
export default IncomeExpensesContainer;
How could value be used just an initial value of the input field?
Thank you!