javascript - 如何使用 api 在 reactjs 中添加搜索功能
问题描述
我正在开发使用 redux 进行状态管理的反应应用程序。在这个应用程序中,我必须实现搜索页面。当用户在搜索框中键入时,应用程序会显示与该文本输入相关的产品。我有这样的 api 端点api.XXXXXXX.com/buyer/product/filter?q=${inputValue}
。我尝试实现这个,但我无法过滤这个。我以前没有做过这方面的工作,也找不到很好的教程。
这是我的代码
searchAction.js
export const searchProduct =(value) => dispatch => {
dispatch({
type: searchActionTypes.SEARCH_PRODUCT_LOAD
});
new _rest().get(`/buyer/product/filter?q=${value}`)
.then(res => {
console.log(res);
dispatch({
type: searchActionTypes.SEARCH_PRODUCT_SUCCESS,
payload: res
})
}).catch(err => {
console.log(err)
dispatch({
type: searchActionTypes.SEARCH_PRODUCT_ERROR,
error: err
})
});
}
searchActionreducer.js
const initialState = {
isFetching: false,
searchData: {},
isFetched: false,
isError: null
}
export default (state=initialState, action) =>{
switch (action.type) {
case searchActionTypes.SEARCH_PRODUCT_LOAD:
return {
...state,
isFetching: true
};
case searchActionTypes.SEARCH_PRODUCT_SUCCESS:
return {
...state,
isFetching: false,
searchData: action.payload.data._embedded,
isFetched: true
};
case searchActionTypes.SEARCH_PRODUCT_ERROR:
return {
...state,
isFetched: false,
};
default:
return {
...state
}
}
}
搜索页面.js
class SearchPage extends React.Component {
state = {
inputValue: '',
}
componentDidMount() {
this.props.searchProduct('');
}
componentDidUpdate(prevProps, prevState, snapshot) {
console.log('component update', prevState)
if(this.state.inputValue !== prevState.inputValue) {
this.props.searchProduct(this.state.inputValue);
}
}
render() {
console.log('SearchPage Result', this.props.product);
const {product} = this.props;
const {inputValue} =this.state;
const updateInputValue = (e) => {
e.preventDefault();
this.setState({
inputValue: e.target.value
})
}
return(
<Grid container>
<div style={{width: '100%', display:'flex', justifyContent: 'center' }}>
<form noValidate autoComplete="off">
<TextField value={inputValue} onChange={updateInputValue} id="standard-basic" label="Search Product" />
</form>
</div>
<Grid container spacing={8} style={{padding: '30px'}}>
{product && product.productResourceList &&
product.productResourceList.map((data, index) => {
return(
<Grid item xs={6}>
<MainCards key={data.productId} data={data} />
</Grid>
)
})}
</Grid>
</Grid>
)
}
}
const mapStateToProps =(state) => {
return {
product: state.search.searchData
}
}
const mapDispatchToProps = {
searchProduct,
}
export default connect(mapStateToProps, mapDispatchToProps)(SearchPage);
componentDidMount
最初,当组件安装时,我显示所有产品。之后,componentDidUpdate
我尝试从 api 购买中获取过滤数据,并将其作为api 端点this.state.inputValue
中的值传递。但是组件根本没有更新。q
api.XXXXXXX.com/buyer/product/filter?q=${inputValue}
解决方案
推荐阅读
- rust - 带有引用的通用参数用作函数指针参数
- javascript - Chrome 扩展 - 使注入的元素打开包含在扩展中的 html 页面
- php - 在php中下载具有特定名称的文件
- node.js - 删除请求nodejs打字稿
- python - Python for-loop in range not providing desired result
- owl - SHACL 规则中的多路径和存在量化(我应该使用 sh:oneOrMorePath 吗?)
- kotlin - 将 Suspended-function 引用作为另一个函数参数传递
- sql - 非规范化后的成本(%CPU)大致相同?
- javascript - 在函数内设置为 null 时的空数组仍然保持相同的 javascript
- qt - QDomElement::text() 没有子元素文本?