获取当前状态时出现Redux异步问题

时间:2018-05-07 16:54:05

标签: javascript reactjs asynchronous redux react-redux

我在组件中获取状态时遇到异步问题。当我在mapStateToProps中控制登录时,它很好但是当我在我的渲染中控制日志时...它就像旧状态一样。如何在redux中链接函数?

const description = document.getElementById("description")

function changeparagraph(msg) {
      description.innerHTML=msg;
  } 

  function originalparagraph() {
    description.innerHTML="This is an awesome content";
  }

2 个答案:

答案 0 :(得分:0)

TL; DR:在配置商店后发送您的操作:

 // const store = createStore(...); // wherever you have this line (often in index.js)
 store.dispatch(actions.fetchListings());

并放置占位符(或不渲染任何内容)直到它结算:

<Routes />
    {this.props.listings.length? <HomePage listings={this.props.listings} />:null}
 ...

NL; PR:

实例化WrapperApp后,Apps render将被调用,然后componentDidMount,因此您的调度调用将在第一次渲染后进行调度。最好尽快调度您的提取(服务器数据请求)操作,它们是非阻塞的。当它们被加载时,在渲染中检查它,并且a)不显示任何内容(null)或b)显示占位符:加载动画,启动画面,消息......

答案 1 :(得分:-1)

绑定from("direct:deleteIds") .to("sql:DELETE FROM TABLE_NAME WHERE ID IN (:#in:idSet)");

中的个别操作
from("direct:deleteIds")
        .setBody(simple("DELETE FROM TABLE_NAME WHERE ID IN (:?in:idSet)")) // Note questionmark here for specifying parameter. This is difference against query in uri.  
        .to("direct:executeQuery");

from("direct:executeQuery")
        .setHeader(SqlConstants.SQL_QUERY, body())
        .to("sql:dummy?dataSource=#datasource");