gpt4 book ai didi

angularjs - React/Redux 等待商店更新

转载 作者:行者123 更新时间:2023-12-05 02:19:00 25 4
gpt4 key购买 nike

我遇到一个问题,即在 redux 存储有任何数据之前, react 组件正在呈现。

问题是在现有 Angular 应用程序将数据分派(dispatch)到商店之前,React 组件被渲染到页面引起的。我不能改变渲染的顺序或类似的东西。

我的简单 React 组件是

import React, {Component} from 'react';
import { connect } from 'react-redux';

import {addBot} from './actions';

class FlowsContainer extends React.Component {

componentDidMount() {
this.props.initStoreWithBot();
}

render() {
// *** at this point I have the store in state prop
//but editorFlow array is not yet instanced, it's undefined

const tasks = this.props.state.editorFlow[0].flow.tasks
return (
<div>
Flow editor react component in main container
</div>
);
}

}

const mapStateToProps = (state) => ({
state : state
})

const mapDispatchToProps = (dispatch) => {
return {
initStoreWithBot : () => dispatch(addBot("test 123"))
};
};

export default connect(
mapStateToProps,
mapDispatchToProps
)(FlowsContainer)

那么我怎样才能推迟渲染直到 editorFlow 数组有元素呢?

最佳答案

您可以使用 Conditional Rendering .

import {addBot} from './actions';

class FlowsContainer extends React.Component {

componentDidMount() {
this.props.initStoreWithBot();
}

render() {
// *** at this point I have the store in state prop
//but editorFlow array is not yet instanced, it's undefined

const { editorFlow } = this.props.state;
let tasks;
if (typeof editorFlow === 'object' && editorFlow.length > 0) {
tasks = editorFlow[0].flow.tasks;
}
return (
{tasks &&
<div>
Flow editor react component in main container
</div>
}
);
}

}

const mapStateToProps = (state) => ({
state : state
})

const mapDispatchToProps = (dispatch) => {
return {
initStoreWithBot : () => dispatch(addBot("test 123"))
};
};

export default connect(
mapStateToProps,
mapDispatchToProps
)(FlowsContainer)

关于angularjs - React/Redux 等待商店更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43803594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com