Reputation: 181
How can I change the state of a component when another changes?
When the state of a select changes AssetType
, the value of the select AssetTypeCategory
must be set to 0
const handleAssetType = (e)=>{
setAssetType(e.target.value),
setAssetTypeCategory(0),
}
<div>
<label htmlFor="AssetType>Asset Type:</label>
{Array.isArray(state.AssetType) ? (
<select
name="AssetType"
id="AssetType"
value={state}
onChange={handleAssetType }
>
<option key="0" value="0">
-- PICK A ITEM--
</option>
{state.AssetType.map((x) => (
<option key={x.id} value={x.id}>
{x.name}
</option>
))}
</select>
) : null}
</div>
Upvotes: 0
Views: 61
Reputation: 192
The code is wrong for two reasons.
1 - You dont need to use comma to separate functions. The code would be:
const handleAssetType = (e)=>{
setAssetType(e.target.value)
setAssetTypeCategory(0)
}
2 - If you do setAssetType(e.target.value)
the value is a int, so AssetType is not an array, making a blank page.
Upvotes: 1
Reputation: 76
Do you using class or functional component ? It looks like you using class component , u cant change class component's state this way , use setState and spread "..." operator.
Upvotes: 2