简体   繁体   English

更新reactjs函数中的redux,然后使用状态

[英]Update redux in the reactjs function, then use the state

I need to open a search aid and select a value there and get it back. 我需要打开一个搜索工具并在其中选择一个值,然后将其取回。

When I click on the button, I open a search help, I put the data I selected into a store, but how can I use it when I come back? 当我单击按钮时,我打开搜索帮助,将选择的数据放入存储中,但是回来后如何使用? I need to write the data I selected from the search help directly into an input on the front side. 我需要将从搜索帮助中选择的数据直接写到正面的输入中。

   async showPopup(){
      const LazyLoadingComponent=await import('../../CP/SearchHelp/searchHelp');
      this.setState({lazyLoadComponent:React.createElement(LazyLoadingComponent.default)});
      await ShowPopup('http://localhost:3070/api/WorkCenter/GetWorkCenters');
      console.log(this.state.selectedRow);
      if(this.state.selectedRow!==''){
        this.setState({WorkCenterCode:this.state.selectedRow.WorkCenterCode});
      }
    }

Here in some way I have to wait until the page is imported. 在这里,我必须以某种方式等待页面被导入。 In the showpopup , I actually show the data that needs to be updated by updating the redux in the search help. showpopup中 ,我实际上显示了需要通过更新搜索帮助中的redux进行更新的数据。

export async function ShowPopup(apiUrl){
    var apiData=await APIGetWorkCenters(apiUrl);
    SearchHelApiData(await JSON.parse(apiData.data));
    SearchHelPopupOpen(true);
}


export const SearchHelPopupOpen=(popupOpen)=>{
    store.dispatch({
        type:'SearchHelp_popupOpen',
        popupOpen:popupOpen
    });
}

export const SearchHelApiData=(apiData)=>{
    store.dispatch({
        type:'SearchHelp_apiData',
        apiData:apiData
    });
}

Here I need to make my searchhelp component async and component until closing. 在这里,我需要使我的searchhelp组件异步并保持关闭状态。 I share the codes of the searchhelp component below. 我在下面分享了searchhelp组件的代码。

class SearchHelp extends BasePage {

  constructor(props){
    super(props);
    this.connect(['SearchHelp']);
    this.onSelectionChanged = this.onSelectionChanged.bind(this);
  }

  componentDidMount(){
    SearchHelSelectedRow('');
  }


    toggle = () => {
      SearchHelApiData('');
      SearchHelPopupOpen(false);
    }

    onSelectionChanged({ selectedRowsData }) {
      const data = selectedRowsData[0];
      SearchHelSelectedRow(data);
      SearchHelApiData('');
      SearchHelPopupOpen(false);
    }

    render() {
      return (
        <MDBContainer>
          <MDBModal  size="md" isOpen={this.state.popupOpen} toggle={this.toggle} centered backdrop={false}>
            <MDBModalHeader className="" toggle={this.toggle}></MDBModalHeader>
            <MDBModalBody>
            <DataGrid
                dataSource={this.state.apiData}
                selection={{ mode: 'single' }}
                showBorders={true}
                hoverStateEnabled={true}
                keyExpr={'WorkCenterId'}
                onSelectionChanged={this.onSelectionChanged} >
              </DataGrid>
            </MDBModalBody>
          </MDBModal>
        </MDBContainer>
        );
    }
}

I'm waiting for your help.. 我正在等待您的帮助。

----------EDIT------------- - - - - - 编辑 - - - - - - -

I solved my problem with the componentDidUpdate() method. 我用componentDidUpdate()方法解决了我的问题。

  componentDidUpdate(){
      if(this.state.selectedRow!=='' && this.state.selectedRow!==undefined){
        SearchHelSelectedRow('');
        if(this.state.selectedRow.WorkCenterId!==undefined){
        this.setState({WorkCenterCode:this.state.selectedRow.WorkCenterCode});}
        if(this.state.selectedRow.ReasonCode!==undefined){
          this.setState({ReasonCode:this.state.selectedRow.ReasonCode});}
      }
    }

    async showPopupWorkCenter(){
      await ShowPopup('http://localhost:3070/api/WorkCenter/GetWorkCenters');
    }

    async showPopupReasons(){
      await ShowPopup('http://localhost:3070/api/Reason/GetReasons');
    }

In order for you to use Redux in your SearchHelp component and gain access to the Redux store, you need to connect your component to the store which I don't see you doing. 为了让您在SearchHelp组件中使用Redux并获得对Redux商店的访问权,您需要将组件连接到我看不到的商店。

You need to three things basically to get things working, a reducer, actionCreator and a store to hold state changes. 从根本上讲,您需要执行三项操作才能使工作正常运行:reducer,actionCreator和用于保存状态更改的存储。 When you have these then you would have to connect your component to the store by using the connect higher order function which takes two arguments and wraps your component giving you access to the data stored in the store. 如果有这些,则必须使用connect高阶函数将组件连接到商店,该函数接受两个参数并包装组件,从而可以访问存储在商店中的数据。

As an example, given your component SearchHelp , you can connect to the store by doing this: 例如,给定组件SearchHelp ,您可以通过执行以下操作连接到商店:

import { connect } from 'redux'

class SearchHelp extends BasePage { ... }

function mapStateToProps(state) {
   // this function has access to your redux store, so you can access the properties there
   // it should return an object
   return {
      stateProp1: state.stateProp1,
      stateProp2: state.stateProp2,
      ...
   }
}

function mapDispatchToProps() {
   // this function is not required as you could simply pass in your actions directly
}
export default connect(mapStateToProps, mapDispatchToProps)(SearchHelp)

An example reducer looks like below: 减速器的示例如下所示:

function reducerName(state = {}, action) {
   switch(action.type) {
      case ACTION_TYPE:
      return { stateProp1: action.data // };
      ...

      default:
      return state;
   }
}

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

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