I have a small React app.
The aim of this project is to manage products.
These products contain features.
The example below is for a page used to configure prices for product features.
export function App() {
// This state is manage by HTTP calls
const [features_dict, set_features_dict] = useState({"features": []});
// I want to display the details of my list
return (
<div className={s.main_container}>
<FeaturesDetail FeaturesDict={features_dict} />
</div>
)
}
My FeaturesDetail component
export function FeaturesDetail({FeaturesDict}) {
const listFeaturesElement = FeaturesDict.features.map(feature =>
<tbody>
<td>{feature.name}</td>
<td>
<input
type="number"
value={feature.price}
/>
</td>
<td><button>Update</button></td>
</tbody>
);
return (
<div>
<table>
<thead>
<td>Name</td>
<td>Price</td>
</thead>
{listIngredientsElement}
</table>
</div>
);
}
It seems like a React event prevent me to modify my input field.
My final goal is to update the features_dict state and run a function to send it in a HTTP call.
This HTTP call’ll update the product configuration in back-end database.
2
Answers
You need to set
onChange
oronInput
event on theinput
element to a function which will update the price, Define the function at theApp
component and pass it toFeaturesDetail
component as a prop ( to be in the place where the state is defined ), this function takes two parameters, the first parameter is a unique identifier for the product, for example, thename
or theid
, the second parameter is the newprice
, then update the product which has this unique identifier with the new price.App.js
FeaturesDetail.js
Also, I suggest that you rename your variables as camelCase for javascript standards
Why it does not work
There are either controlled inputs or uncontrolled input fields. Whenever you pass in a
value
attribute to an input field in React, you indicate that it is controlled and you will be handling the value changeThe above means that your code should update
inputVal
so that the input field shows it.And for that you have to add a change handler :
This changeHandler updates the inputVal and triggers a rerender to show the updated inputVal in the input text field.
The above is for controlled. If you think that the field is better of uncontrolled, then all you have to do is change to this:
Simple demo
In your case you want to have a controlled field and update its state using handlers so please follow the guide linked above.