如何在组件中发布http请求并更新它?



这些天,我正在使用reactredux,但是我遇到了发布http请求的问题,我正在使用axios发布http请求,并尝试了很多次或从互联网上演示。它们在我的程序中都不起作用。我只想调用一些 API 并更新我的组件。

而且我总是收到错误TypeError getsimpleresult is not a function,并且无法调用此函数,我知道这只是一个小案例。

谁能帮我修复它并给我一个正确的演示。谢谢。

这是我的代码:

减速器.js

const initState = {
simple_result_value: {}
};
export default function simple_result_reduce(state = initState, action) {
switch (action.type) {
case 'query_evaluation_simple_result':
return {
...state,
simple_result_value: action.simple_result_value
};
default:
return state;
}
}

操作.js

const simpleresultaction = (data) => ({
type: 'GET_VALUE',
result_value: data
});

export const getsimpleresult = () => async(dispatch, getState) => {
try {
let response = await post_http("/some/api", params);
await dispatch(simpleresultaction(response.data))
} catch (error) {
console.log('HTTP post error: ', error)
}
};

组件.js

import React, {PropTypes, Component} from 'react'
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import * as simpleActions from '../actions/simple_reuslt'
@connect( state => state,dispatch => bindActionCreators({simpleActions}, dispatch) )
export default class Simple_result_component extends Component {
constructor(props) {
super(props);
}
componentWillMount() {
this.props.getsimpleresult();
}
render() {
return (
<header className="header">
</header>
)
}
}

正如我所看到的,您正在使用bindActionCreators将动作绑定到道具,simpleResult但是您直接使用该函数。我建议你应该像提取它一样

componentWillMount() {
this.props.simpleActions.getsimpleresult();
}

还可以尝试将connect更改为

@connect(state => (state), dispatch => ({simpleActions: bindActionCreators(simpleActions, dispatch)})) 

您的代码示例看起来不错。我想你的行动路径是错误的:

import * as simpleActions from '../actions/simple_reuslt'

你能simple_reuslt检查文件名吗?这里我的意思是错误的写作

最新更新