i am trying to fetch api-data as per user input field. how to fetch the value of these data?
my api end-point like this "http://localhost:8000/api/p_list?search=" . whenever user input the value, endpoint is like this "http://localhost:8000/api/p_list?search=01" here is the input field value is "01". i want to get the value of the result.
i am probably new to reactjs. i tried something below like this but this is the static API url what i'm trying to fetch. i am not sure how to fetch dynamic url as per user input field.
it would be great if anybody could help me out what i am trying to solve. thank you so much in advance.
./src/productList.js
import React, {Component} from "react";
import Contacts from './productListHook.js';
export default class App extends Component{
state = {
contacts: []
}
componentDidMount() {
fetch('http://localhost:8000/api/p_list?search=')
.then(res => res.json())
.then((data) => {
this.setState({ contacts: data })
})
.catch(console.log)
}
render(){
return(
<Contacts contacts={this.state.contacts} />
)
}
}
./src/ProductListHook.js
import React from 'react'
const Contacts = ({ contacts }) => {
return (
<section class="product_list section_padding">
<div class="container">
<div class="row">
<div class="col-md-3">
<div class="product_sidebar">
<div class="single_sedebar">
<form action="#">
<input type="text" name="#" placeholder="Search keyword"/>
<i class="ti-search"></i>
</form>
</div>
</div>
</div>
<div class="col-sm-9">
<div class="product_list">
<div class="row">
{contacts.map((contact) => (
<div class="col-lg-3 col-md-9">
<div class="single_product_item">
<img src={contact.image} alt="" class="img-fluid" />
<h3> <a href={contact.url}>{contact.title}</a> </h3>
<p>From ${contact.price}</p>
</div>
</div>
))}
</div>
<div class="load_more_btn text-center">
<a href="#" class="btn_3">Load More</a>
</div>
</div>
</div>
</div>
</div>
</section>
)
};
export default Contacts