I am very new to Reactjs. I am taking a course in Reactjs and trying by myself. So kindly excuse me.
- In the below code I have two list boxes. I am selecting the postcode and based on the post code the other list box needs to be populated
- In order to achieve this I used useEffect but made sure I passed empty array at the end so that it will not be called every time the state data and pc are updated
- I read the other similar post and did #2.
- Clearly the renders are in loop but which one?
- I see many questions similar to mine in stackoverflow and out of which solution in #2 seems to be related to mine but I would like to know more about #4. Any leads are much appreciated. Thanks
function MainCard(){
const [data, setData] = useState({took: {}, timed_out: false, _shards: [], hits:
{total: {}, max_score: {}, hits: []}});
const [pc, setPc] = useState("");
useEffect(() => {
axios.get("http://localhost:9200/first_name/_search")
.then(response => {
setData(response.data)
});
}, []);
return (
<div>
<Card className="customCard">
<Card.Body>
<select name="postcode" id="postcode">
{data.hits.hits.map(hit => {
setPc(hit._id);
return hit;
})
.map(hit => <option value={hit._id}>{hit._id}</option>)
}
</select>
<select name="type-of-service" id="type-of-service">
{data.hits.hits.filter(hit => hit._id===pc)
.map(hit =>
<option value={hit._source}>{hit._source}</option>
)}
</select>
</Card.Body>
</Card>
</div>
)
}
export default MainCard;