reactjs - Redux/ React Error: Actions must be plain objects. Use custom middleware for async actions
问题描述
I create store as below:
import { createStore, applyMiddleware } from 'redux';
import { logger } from 'redux-logger';
import rootReducer from './reducers/rootReducer';
import { compose } from 'redux';
const InitialState = {
texts: [],
item: {}
}
const store = createStore(
rootReducer,
InitialState,
applyMiddleware(logger),
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
)
export default store;
And this is my userActions file:
import { ADD_TEXT } from './types';
export const addText = () => (dispatch) => {
const obj = [
{
id: 0,
type: 'Apacze',
text: 'przykaldowyTeksciorNa start'
},
{
id: 1,
type: 'Apacze',
text: 'przykladowyTekst2'
}
]
dispatch({
type: ADD_TEXT,
payload: obj
})
}
This is my userReducer as below:
import { ADD_TEXT, LISTED_USER } from '../actions/types';
const InitialState = {
texts: [],
item: {}
}
export const userReducer = (state=InitialState, action) => {
switch(action.type) {
case ADD_TEXT: {
return {
...state,
texts: action.payload
}
}
case LISTED_USER: {
return {
...state,
items: action.payload
}
}
default: {
return state
}
}
}
This is my React component, where I using my Redux action:
import React, { Component } from 'react';
import ApaczeView from '../view/ApaczeView';
import { addText } from '../redux/actions/userActions';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
class Apacze extends Component {
constructor(props) {
super(props);
this.state = {
text: 'Dokończ zaczętą historie, to od ciebie zależy jak potoczą się dalsze losy bohaterów.'
}
this.changeTekst = this.changeTekst.bind(this);
this.addText = this.addText.bind(this);
}
changeTekst(e) {
this.setState({
[e.target.getAttribute('name')]: [e.target.innerHTML]
})
}
addText(e) {
this.props.dispatch(addText());
}
render() {
return(
<ApaczeView text={this.state.text} changeTekst={this.changeTekst} addText={this.addText} />
)
}
}
Apacze.PropTypes = {
dispatch: PropTypes.func.isRequired,
texts: PropTypes.array.isRequired
}
const mapStateToProps = (state) => ({
texts: state.users.texts
})
export default connect(mapStateToProps ) (Apacze);
I tried almost everything including with similar solving questions from stackoverflow. Unfortunately none of similar solving questions don't work in my case.
解决方案
您的 addtext 操作不是普通对象,如果 obj 是 const 您可以将其停止操作
import { ADD_TEXT } from './types';
const obj = [
{
id: 0,
type: 'Apacze',
text: 'przykaldowyTeksciorNa start'
},
{
id: 1,
type: 'Apacze',
text: 'przykladowyTekst2'
}
]
export const addText = () => ({
type: ADD_TEXT,
payload: obj
})
否则你可以使用 redux thunk 作为中间件
import thunk from 'redux-thunk';
const store = createStore(
rootReducer,
InitialState,
applyMiddleware(thunk, logger),
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
)
推荐阅读
- javascript - JS - Check if element exist in loop
- java - MongoDb aggregates group Arrays object
- amazon-web-services - 使用 IoT Core 操作时使用 AWS DynamoDB V2 的服务器端时间戳?
- angular - Angular 5 - Api Swagger body responseType blob
- javascript - Javascript:更改输入文件类型?
- java - Thymeleaf:通过索引从数组列表中获取项目
- javascript - Joi - 从数组的第一个元素访问对象项属性
- windows - Mattermost Webhook (Windows):使用 cURL 将表格发送到频道
- powershell - 如何使用 PowerShell 从 Azure 函数查询 Azure 表
- amazon-web-services - 如何在 EC2 上定义 ssh 主机密钥以避免连接时验证失败?