I'm trying to use a fetched value from DB as a default value (shown until the component is clicked) of an input field.
However, using "defaultValue" attribute, the fetched value it is not displayed.
App.tsx file (value is fetched successfully):
const [Income, setIncome] = useState<string>('0');
useEffect(() => {
const incomeResp = async () => {
await axios.get('http://localhost:4000/app/income')
.then(
result => setIncome(result.data && result.data.length > 0 ? result.data[0].income : 0))
}
incomeResp();
}, []);
return (
<div className="App">
<div>
<IncomeExpensesContainer
income={Income}
setIncome={setIncome}
totalExpenses={TotalExpensesAmount}
currencySymbol={Currency}
/>
</div>
Component's file:
interface Props {
income: string;
setIncome: (value: string) => void;
totalExpenses: number;
currencySymbol: string;
}
const IncomeExpensesContainer: React.FC<Props> = ({
income,
setIncome,
totalExpenses,
currencySymbol,
}: Props) => {
const [insertedValue, setInsertedValue] = useState<string>(income);
return (
<Grid container spacing={1} className="income-expenses-container">
<InputItem
onChange={setInsertedValue}
onBlur={setIncome}
title="Income"
type="number"
placeholder="Your income"
defaultValue={income}
/>
</Grid>
);
What am I missing here?