Javascript Redux/React路由器-能够发送URL';具有客户端路由的参数的
我希望在给定搜索参数url时,能够正确地填充Redux状态 目前我的流程如下: 搜索->Javascript Redux/React路由器-能够发送URL';具有客户端路由的参数的,javascript,reactjs,express,redux,react-router,Javascript,Reactjs,Express,Redux,React Router,我希望在给定搜索参数url时,能够正确地填充Redux状态 目前我的流程如下: 搜索-> <Redirect to={{ pathname: '/search', search: `?item-name=${this.props.searchQuery}` }} /> 因此,该对象在Redux状态的搜索结果中可用,因为我已经连接了它 function mapStateToProps(state) { return { // Gets the list of fe
<Redirect to={{
pathname: '/search',
search: `?item-name=${this.props.searchQuery}`
}} />
因此,该对象在Redux状态的搜索结果中可用,因为我已经连接了它
function mapStateToProps(state) {
return {
// Gets the list of fetched items
items: state.items.items
};
}
如果我给一个朋友https://mywebsite.com/search?item-name=foo
my“items”状态将为空,因为它没有执行所有要调度的操作,因此从技术上讲,它没有从my db获取信息
我是否可以允许在输入上述url时请求此项目信息,同时仍在客户端执行Redux/路由操作?这类似于在刷新页面时这样说
https://mywebsite.com/search?item-name=foo
您的axios呼叫不会被解雇
您需要做的是创建如下内容:
componentDidMount() {
const {items} = this.props; // assuming you can check whether items is populated
if (!items) {
this.props.dispatch(
makeSearchResultsAxiosCall(this.props.location.query.item-name)
)
}
// use this.props.location.query to access the query parameter
}
如果页面被刷新或有人直接导航到它,这将触发请求。谢谢您的回答,这只是一个关于正确编码实践的快速问题。是否允许在组件内部分派操作?我总是在redux示例中看到调度是在操作中完成的。(除非我也必须为此分派一个新函数)。是的,您肯定可以从组件内部分派。
componentDidMount() {
const {items} = this.props; // assuming you can check whether items is populated
if (!items) {
this.props.dispatch(
makeSearchResultsAxiosCall(this.props.location.query.item-name)
)
}
// use this.props.location.query to access the query parameter
}