简体   繁体   English

使用 axois 渲染 redux 动作时未捕获的 Promise

[英]Uncaught Promise when rendering redux action with axois

Hi I am using redux to do some API calls when trying to render the json response.嗨,我在尝试呈现 json 响应时使用 redux 来执行一些 API 调用。 I am trying to log the responses to work out what is happening but even that is not working?我正在尝试记录响应以找出正在发生的事情,但即使这样也不起作用?

dashaction.js dashaction.js

export function updateyield(total){
    return{
        type:"UPDATE_YIELD",
        total: total
    }
}

export function loadyield(){
    return(dispatch)=>{
        return axios.get("localhost:5000/getallyield").then ((response) => {
            dispatch(updateyield(response.data));
            let res = response.data;
            console.log(res.data)
        })
    }
}

this is rendered in DashContent这是在DashContent 中呈现的

class DashContent extends Component {

    render () {
        return(

        <div>
        {this.props.loadyield()}
        <h3> Yield</h3>
        <ul>{this.props.total}</ul>
        </div>
        );
    }

}
function mapStatetoProps(state) {
    return state
    };

export default connect(mapStatetoProps, {loadyield}) (DashContent);

Here is the full error message:这是完整的错误消息:

Error: Objects are not valid as a React child (found: [object Promise]). If you meant to render a collection of children, use an array instead.
    in div (at dashboardcontainer.js:12)
    in DashContent (created by ConnectFunction)
    in ConnectFunction (at Dashboard.js:15)
    in div (at Dashboard.js:14)
    in Dashboard (created by Context.Consumer)
    in Route (at App.js:16)
    in Switch (at App.js:15)
    in div (at App.js:13)
    in Router (created by BrowserRouter)
    in BrowserRouter (at App.js:12)
    in App (at src/index.js:13)
    in Provider (at src/index.js:13)

Is this an error with my request?这是我请求的错误吗? or how I am rendering the response?或者我如何呈现响应? from my API log nothing seems to have been recieved?从我的 API 日志中似乎没有收到任何信息? apologies if I have not provided enough infomation如果我没有提供足够的信息,请道歉

The error here is that you are calling the loadyield() function in the returned JSX tag这里的错误是你在返回的 JSX 标签中调用了loadyield()函数

You can load the data once the component mounts.您可以在组件安装后加载数据。 You can do this by adding a componentDidMount() lifecycle method.您可以通过添加componentDidMount()生命周期方法来实现。 So your DashContent Would look something like this.所以你的DashContent看起来像这样。

class DashContent extends Component {
    componentDidMount(){
        this.props.loadyield();
    }
    render () {
        return(
        <div>
            <h3> Yield</h3>
            <ul>{this.props.total}</ul>
        </div>
        );
    }

}

function mapStatetoProps(state) {
    return state
};

export default connect(mapStatetoProps, {loadyield}) (DashContent);

Alternatively you can use componentWillRecieveProps (this lifecycle method has been deprecated. It can be replaced like this ) or simply have a refresh button which dispatches the action to update the yield.或者,您可以使用componentWillRecieveProps (此生命周期方法已被弃用。它可以像这样替换)或简单地使用刷新按钮来调度操作以更新产量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM