大家晚安,我想做的就是通过loadUA函数中的bind注入axios,但是当默认导入组件时,出现此错误。原因是我想消除方法中的轴依存关系,以便我可以更轻松地执行测试,而无需在加载中进行绑定,一切正常,但是我无法通过绑定注入轴并在组件中使用该方法。
伙伴
import React from 'react';
import { connect } from 'react-redux';
import loadUA from './actions'
class UserAgent extends React.Component {
componentDidMount(){
this.props.loadUA()
}
render(){
if(this.props.isFetching){
return <span> Loading ...</span>
}
if(this.props.error){
return <span>Error</span>
}
return (
<span>{this.props.data['user-agent']}</span>
)
}
}
const mapStateToProps = (state) => {
return {
isFetching: state.UA.isFetching,
data: state.UA.data,
error: state.UA.error
}
}
const mapDispatchToProps = (dispatch) =>{
return {
loadUA: () => dispatch(loadUA())
}
}
export default connect( mapStateToProps, mapDispatchToProps )(UserAgent)
动作
import axios from 'axios'
export const loadUARequest = () => {
return {
type: 'LOAD_UA_REQUEST'
}
}
export const loadUASuccess = (data) => {
return{
type: 'LOAD_UA_SUCCESS',
data
}
}
export const loadUAError =() =>{
return{
type: 'LOAD_UA_ERROR'
}
}
export const loadUA = (axios) => {
return dispatch => {
dispatch(loadUARequest())
axios
.get('https://httpbin.org/user-agent')
.then(({data}) => dispatch(loadUASuccess(data)))
.catch(() => dispatch(loadUAError()))
}
}
export default {
loadUA: loadUA.bind(null, axios)
}
错误