简体   繁体   English

React Redux如何使用redux状态进行条件渲染

[英]React Redux how make conditional rendering with redux state

i have some problem i can't understand how figure out with it 我有一些问题,我无法理解如何弄清楚它

I'm using react-bootstrap tabs i have 3 tab in my tab panel and in each tab i have an api request for getting data. 我正在使用react-bootstrap选项卡我的选项卡面板中有3个选项卡,在每个选项卡中我都有api请求获取数据。 Each tab data i'm getting via redux actions so i have 3 redux actions. 我通过redux动作得到的每个标签数据,所以我有3个redux动作。

on each component in componentDidMount i'm dispatching my actions which getting my data via api 在componentDidMount中的每个组件上,我正在调度我通过api获取数据的操作

everything work well. 一切顺利。

but i wan't to prevent clicking to my tabs while one tab data request continue. 但是,当一个标签数据请求继续时,我不想阻止点击我的标签。 When request is finished then users can click another tab and on another tab it will be same scenario. 请求完成后,用户可以单击另一个选项卡,在另一个选项卡上,它将是相同的方案。

i have a parent component where my tabs navigation: 我有一个父组件我的标签导航:

<Tabs
            id="controlled-tab-example"
            activeKey={this.state.key}
            onSelect={key => this.setState({ key })} 
          >
            <Tab eventKey="a" mountOnEnter={true} disabled={this.props.datatableA.isLoaded}  title="Data Table A">
              <DataTableA />
            </Tab>
            <Tab eventKey="b" mountOnEnter={true} disabled={this.props.datatableB.isLoaded} title="Data Table B">
              <DataTableB />
            </Tab>
            <Tab eventKey="c" mountOnEnter={true} disabled={this.props.datatableC.isLoaded}  title="Data Table C">
              <DataTableC />
            </Tab>
          </Tabs>

here is with disabled attribute i'm making disable clicking conditionally my button if data isLoaded 这里是禁用属性我正在禁用有条件地点击我的按钮,如果数据已加载

let initialState = [{
    isLoaded: false
}]

export default (state = initialState, action) => {
    switch (action.type) {
        case FETCH_DATA_START:
            return {...state, isLoaded: true}
        case FETCH_DATA_SUCCESS:
            return {...state, items: action.payload, isLoaded: false}
        case FETCH_DATA_FAILED: 
            return {...state}
        default:
            return state
    }
}

so here in my reducer if data fetch start my isLoaded state is true so button is disable when success isLoaded is false so button is clickable. 所以在我的reducer中,如果数据提取开始,我的isLoaded状态为true,因此当成功isLoaded为false时按钮被禁用,因此按钮是可点击的。

but here problem is component getting the state from store once so when state updated on my store, i can't catch it and because of them my conditionally rendering not working. 但这里的问题是组件从商店获取状态一次,所以当我的商店更新状态时,我无法抓住它,因为他们我的条件渲染无法正常工作。

how to do that? 怎么做?

Inside render deconstruct props which you are getting from mapStateToProps 内部渲染解构你从mapStateToProps获得的道具

Const {dataTableA, dataTableB, dataTableC} = this.props; Const {dataTableA,dataTableB,dataTableC} = this.props;

Then in every tab disabled condition should have all three datatable's loaded conditions, so that if any of the api calls are dispatched and not returned. 然后在每个选项卡中禁用条件应该具有所有三个数据表的加载条件,以便在调度任何api调用时不返回。 No tab should be enabled 不应启用任何标签

// this will be changed 
this.props.dataTableA.isLoaded
//to
dataTableA.isLoaded || dataTableB.isLoaded || dataTableC.isLoaded

P. S also check your changed states by adding console in render. P. S还通过在渲染中添加控制台来检查已更改的状态。

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

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