【发布时间】:2021-03-09 21:19:17
【问题描述】:
我正在尝试构建一个条件动态反应组件,在该组件中基于用户交互进行 API 调用,但如果用户在搜索栏中键入内容。我想添加search= 参数,否则使用/list 没有查询参数的端点。我目前正在使用 Axios ,我想知道一些方法来执行以下操作
const FeedsList = () => {
const [feed, setFeed] = useState([]);
const [currentPageUrl, setCurrentPageUrl] = useState("http://localhost:8001/api/v1/feeds/list/")
const performSearch = () => {
//setLoading(true)
api.get(currentPageUrl).then(res => { // axios call
setLoading(false)
setFeed(res.data.results)
}).catch(function(error){
console.log(error);
});
}
const handleSearch = (e) =>{
console.log(e.target.value)
//performSearch();
}
useEffect(() => {
performSearch()
}, [currentPageUrl]);
if (loading) return "Loading..."
}
export const api = axios.create(
{baseURL : 'http://localhost:8001/api/v1/feeds/list/'}
)
用户输入
<input type="text" placeholder="Enter keyword" onChange={event => handleSearch(event)}/>
【问题讨论】:
-
处理用户输入的其余代码在哪里?
-
@Nick 更新了,我忘记了用户输入
-
input元素是由父元素渲染的吗?如果是这样,那么这需要在那里处理