javascript - 动作不触发reducer
问题描述
我正在尝试实现搜索,并且每次输入文本以搜索输入时,我都会执行输入操作,但减速器不会触发,我不明白为什么,最奇怪的是,另一个动作和减速器可以正常工作,但只有一个不能。
搜索组件
import React, {Component} from 'react'
import './search.css';
import axios from 'axios'
import {getPlayList,setSearchValue} from "./actions";
import {connect} from "react-redux";
class Search extends Component {
state = {
query: '',
results: []
};
getInfo = () => {
this.props.getPlayList(this.state.query);
console.log(this.props.search.searchPlayList);
};
handleInputChange = () => {
/*this.setState({
query: this.search.value
}, () => {
if (this.state.query && this.state.query.length > 1) {
this.getInfo();
}
});*/
setSearchValue(this.search.value);
console.log(this.props.search.searchValue);
};
render() {
return (
<form>
<input
placeholder="Search for..."
ref={input => this.search = input}
onChange={this.handleInputChange}
/>
</form>
)
}
}
const mapStateToProps = store => {
return {
search: store.search
};
};
export default connect(mapStateToProps, {getPlayList,setSearchValue})(Search);
行动方法:
export const getPlayList = (queryParam) => {
return function (dispatch) {
dispatch({type: FETCH_PLAYLIST_REQUEST});
axios.get(`https://cors-anywhere.herokuapp.com/https://api.deezer.com/search/track?q=${queryParam}`).then(response => {
dispatch({
type: FETCH_PLAYLIST_SUCCESS,
payload: {
playlist: response.data.data,
currentTrack: response.data.data[0]
}
});
}).catch(err => {
dispatch({
type: FETCH_PLAYLIST_FAILURE,
payload: err,
});
})
}
};
export const setSearchValue = (value) => {
console.log('setSearchValue',value);
return function (dispatch) {
dispatch({
type: FETCH_SEARCH_VALUE,
payload: value,
});
}
}
减速器
import {
FETCH_PLAYLIST_FAILURE,
FETCH_PLAYLIST_REQUEST,
FETCH_PLAYLIST_SUCCESS,
FETCH_SEARCH_VALUE
} from "../actions/types";
const initialState = {
searchPlayList:null,
currentTrack:null,
searchValue:null,
index: 0,
isLoading: false,
};
export default function (state = initialState, action) {
switch (action.type) {
case FETCH_PLAYLIST_REQUEST:
return {
...state,
isLoading: true
};
case FETCH_PLAYLIST_SUCCESS:
return {
...state,
searchPlayList: action.payload.playlist,
errors: null,
isLoading: false,
};
case FETCH_PLAYLIST_FAILURE:
return {
...state,
errors: action.payload
};
case FETCH_SEARCH_VALUE:
console.log('action.payload',action.payload);
return {
...state,
searchValue: action.payload
};
default:
return state
}
}
解决方案
您必须更改此行:
setSearchValue(this.search.value);
对此:
this.props.setSearchValue(this.search.value);
setSearchValue
in 道具会将动作发送到商店。仅仅调用setSearchValue
本身并没有做任何事情,因为它还没有连接到 redux。
推荐阅读
- php - 使用 PHP "preg_match" 打印不需要的字符
- google-apis-explorer - 如何在 Google FactCheck API 上获取最近的事实检查?
- python - NameError:名称“数据包”未定义 - RLBot
- javascript - babel 插件如何设置 Set 对象(new Set(someArray) 可迭代)
- reactjs - 警告:道具类型失败:提供给“withStyles(SingleDatePicker)”的“object”类型的无效道具“focused”,应为“boolean”
- python - 在构建不和谐机器人时,我可以在 discord.py 中创建两个同名但参数不同的函数吗?
- node.js - 尝试使用 mongodb 制作 todolist 并使用节点表达
- python - 在python中仅使用一个装饰器使用定义的属性并将n个属性传递给包装器
- html - 旋转到横向时,iPad Pro 上的 HTML 元素不会改变宽度
- arrays - 在 dart 中解析对象(不支持的操作:无法添加到固定长度列表)