我使用的是没有钩子的Redux,所有这些似乎都完美地结合在一起,但当我在浏览器控制台Redux窗口中查看时,我的状态不会改变。所以基本上我有一个商店文件,看起来像这个
import {createStore, applyMiddleware} from "redux";
import thunk from 'redux-thunk'
import {composeWithDevTools} from "redux-devtools-extension/developmentOnly";
import rootReducer from './reducers'
const middleware = [thunk]
const initialState = {}
const store = createStore(rootReducer, initialState,composeWithDevTools(applyMiddleware(...middleware)))
export default store
然后我有我的全局还原文件
import {combineReducers} from "redux";
import searchReducer from './searchReducer'
export default combineReducers({
books: searchReducer
})
和searchReducers文件
import {SEARCH_BOOK, SET_INDEX,FETCH_BOOKS} from "../actions/types";
const initialState = {
query: '',
books: [],
loading: false,
book: []
}
export default function (state = initialState, action) {
switch (action.type) {
case 'SEARCH_BOOK':
return {
...state,
query:action.payload,
loading: false
}
case 'SET_INDEX':
return {
...state,
index:action.payload,
loading: false
}
case 'FETCH_BOOKS':
return {
...state,
index:state.index+40,
books:state.books.concat(action.payload),
loading: false
}
default:
return state
}
}
现在我只导入了你看到的动作类型这是动作
import {SEARCH_BOOK} from "./types";
export const searchBook = query => dispatch => {
dispatch ({
type:SEARCH_BOOK,
payload:query
})
}
export const fetchBooks = (query,index) => {
console.log(query)
axios
.get(`https://www.googleapis.com/books/v1/volumes?q=${query}&maxResults=40&orderBy=relevance&startIndex=${index}`)
.then(response =>{
return({
type: FETCH_BOOKS,
payload: response.data.items
})}
)
.catch(err => console.log(err));
};
所有这些都在应用程序中捆绑在一起,我在其中导入了包装所有内容的提供商。问题来了。我有一个搜索表单,应该在更改时更新全局状态的查询值
import React, { useState, useReducer} from "react";
import {useSelector, useDispatch} from 'react-redux'
import { Button, Container, Row, Col, Form, FormGroup, FormInput } from "shards-react";
import queryBuilder from "../js/helper";
import style from "./SearchForm/body.module.css";
import {searchBook, fetchBooks} from "../actions/SearchActions";
const initialState = {
title:'',
author:'',
publisher:''
}
function reducer(state,{ field, value }){
return {
...state,
[field]: value
}
}
function SearchForm() {
const index = useSelector(state => state.index)
const [state, dispatch] = useReducer(reducer, initialState);
const [query, setQuery] = useState('');
const disp = useDispatch();
const onChange = e => {
dispatch({ field: e.target.name, value: e.target.value })
}
const { title,author,publisher } = state;
const handleSubmit = e => {
e.preventDefault()
setQuery(queryBuilder(state))
disp(fetchBooks(query, 0))
}
return(
<div>
<Container className={style.FormContainer}>
<Form onSubmit={handleSubmit}>
<Row className={'topBar'}>
<Col>
<FormGroup>
<FormInput id={'bookTitle'} name={'title'} placeholder={'title'} value={title} onChange={onChange}/>
</FormGroup>
</Col>
<Col>
<FormGroup>
<FormInput id={'bookAuthor'} name={'author'} value={author} onChange={onChange} placeholder={'author'}/>
</FormGroup>
</Col>
<Col>
<FormGroup>
<FormInput id={'bookPublisher'} name={'publisher'} value={publisher} onChange={onChange}
placeholder={'publisher'}/>
</FormGroup>
</Col>
<Col>
<Button outline theme='primary' type={'submit'}>Submit</Button>
</Col>
</Row>
</Form>
</Container>
</div>
)
}
export default SearchForm
我不知道少了什么。
编辑按照建议,我尝试使用钩子,现在一切都很好。现在的问题出在取书上。我更新了操作文件,以便您可以看到我添加的操作。当我发送此操作时,我会收到此错误
操作必须是纯对象。使用自定义中间件进行异步操作
有人知道如何解决这个问题吗?
我想您的错误可以作为解决
export const fetchBooks =(query,index) => dispatch => {
console.log(query)
axios
.get(`https://www.googleapis.com/books/v1/volumes?q=${query}&maxResults=40&orderBy=relevance&startIndex=${index}`)
.then(response =>{
dispatch({
type: FETCH_BOOKS,
payload: response.data.items
})}
)
.catch(err => console.log(err));
};
在fetchBooks
函数中似乎缺少一个return
。您没有返回promise,这意味着thunk中间件没有收到promise结果。
export const fetchBooks = (query,index) => {
console.log(query)
return axios
.get(`https://www.googleapis.com/books/v1/volumes?q=${query}&maxResults=40&orderBy=relevance&startIndex=${index}`)
.then(response =>{
return({
type: FETCH_BOOKS,
payload: response.data.items
})}
)
.catch(err => console.log(err));
};