Javascript 错误:操作必须是普通对象。在删除按钮中使用自定义中间件进行异步操作?
在用户从列表中删除一个文件后,我试图获取一个react操作来获取一个文件列表 在App.js中,我将handleClick函数传递给嵌套组件 App.js 然而,我得到以下错误 错误:操作必须是普通对象。使用自定义中间件进行异步操作Javascript 错误:操作必须是普通对象。在删除按钮中使用自定义中间件进行异步操作?,javascript,reactjs,redux,Javascript,Reactjs,Redux,在用户从列表中删除一个文件后,我试图获取一个react操作来获取一个文件列表 在App.js中,我将handleClick函数传递给嵌套组件 App.js 然而,我得到以下错误 错误:操作必须是普通对象。使用自定义中间件进行异步操作 实现此功能的最佳方法是什么?一个操作将分派另一个操作,但不会分派事件处理程序函数 您不需要从组件中分派deleteFileById,因为这是在操作中导出的函数,它将分派操作 请删除handleClick中的调度以开始工作 错的一个: handleClick = fi
实现此功能的最佳方法是什么?一个操作将分派另一个操作,但不会分派事件处理程序函数 您不需要从组件中分派deleteFileById,因为这是在操作中导出的函数,它将分派操作 请删除handleClick中的调度以开始工作 错的一个:
handleClick = fileId => {
this.props.deleteFileById(dispatch(this.props.dispatch,fileId));
};
正确的一点:
handleClick = fileId => {
this.props.deleteFileById(this.props.dispatch,fileId);
};
关于这一点。props.deleteFileById不是一个函数
有许多方法可以访问组件中的操作。下面是一些方法
你需要安装道具类型
import React, { Component } from 'react';
import { connect } from 'react-redux';
import * as actions from '../actions';
class Test extends Component{
render(){
return(
<div</div>
)
}
}
export default connect(null, {...actions})(Test);
npm安装-s道具类型
import React, { Component } from 'react';
import { connect } from 'react-redux';
import * as actions from '../actions';
class Test extends Component{
render(){
return(
<div</div>
)
}
}
export default connect(null, {...actions})(Test);
如果您的组件是测试组件,则按如下所示设置道具类型
import PropTypes from 'prop-types';
import React, {Component} from 'react';
class Test extends Component{
render(){
return(
<div</div>
)
}
}
Test.propTypes = {
deleteFileById: PropTypes.func
}
谢谢,但现在它说的是>类型错误:\ u this.props.deleteFileById不是一个函数。您需要使用propTypes将deleteFileById定义为一个函数。或者,如果您使用的是redux connect,那么请使用spread operator访问所有操作更新我的答案,请立即查看谢谢,刚刚更新了我的问题并进行了更改,仍然是相同的问题您不了解我的兄弟。你又犯了同样的错误给我两分钟让我更正你的密码
import PropTypes from 'prop-types';
import React, {Component} from 'react';
class Test extends Component{
render(){
return(
<div</div>
)
}
}
Test.propTypes = {
deleteFileById: PropTypes.func
}
import React, { Component } from 'react';
import { connect } from 'react-redux';
import * as actions from '../actions';
class Test extends Component{
render(){
return(
<div</div>
)
}
}
export default connect(null, {...actions})(Test);
import React, { Component } from 'react';
import { connect } from 'react-redux';
import * as actions from '../actions';
import {push} from 'react-router-redux';
class Test extends Component{
static get propTypes() {
return {
sendContactForm: React.PropTypes.func
}
}
render(){
return(
<div</div>
)
}
}
const actionsToProps = {
deleteFileById: actions.deleteFileById,
push
}
export default connect(null, actionsToProps)(Test);
class App extends Component {
static propTypes = {
files: PropTypes.array.isRequired,
isFetching: PropTypes.bool.isRequired,
deleteFileById : PropTypes.func,
fetchFiles: PropTypes.func
};
componentDidMount() {
this.props.fetchFiles();
}
handleClick = fileId => {
this.props.deleteFileById(fileId);
};
render() {
const {files, isFetching} = this.props;
const isEmpty = files.length === 0;
return (
<div>
<h1>Uploadr</h1>
{isEmpty
? (isFetching ? <h2>Loading...</h2> : <h2>No files.</h2>)
: <div style={{opacity: isFetching ? 0.5 : 1}}>
<Files files={files} handleClick={this.handleClick}/>
</div>
}
</div>
)
}
}
const mapStateToProps = state => {
const {isFetching, items: files} = state.files;
return {
files,
isFetching,
}
};
export default connect(mapStateToProps)(App)
import ajax from '../ajax';
import {Map, fromJS} from 'immutable';
import config from '../config';
import {push} from 'react-router-redux'
export const URL_PREFIX = 'http://localhost:3000/api';
export const SEND_CONTACT_FORM_REQUEST = 'SEND_CONTACT_FORM_REQUEST';
export const SEND_CONTACT_FORM_SUCCESS = 'SEND_CONTACT_FORM_SUCCESS';
export const SEND_CONTACT_FORM_ERROR = 'SEND_CONTACT_FORM_ERROR';
export function sendContactFormRequest(){
return {
type: SEND_CONTACT_FORM_REQUEST,
loading: true
}
}
export function sendContactFormSuccess(data){
return {
type: SEND_CONTACT_FORM_SUCCESS,
loading: false,
data: data
}
}
export function sendContactFormError(errors){
return {
type: SEND_CONTACT_FORM_ERROR,
loading: false,
errors: errors
}
}
export function sendContactForm(firstName, lastName, email, subject, message) {
return dispatch => {
dispatch(sendContactFormRequest());
return ajax.post(URL_PREFIX + '/communication/contact', { firstName, lastName, email, subject, message })
.then(res => {
dispatch(sendContactFormSuccess(res.data))
})
.catch(errors => {
dispatch(sendContactFormError(errors))
})
}
}