如何在 react-redux 中获得从 child 到 parent class 的回调?

how to get callback from child to parent class in react-redux?

我必须渲染几个标签。点击它,它应该被突出显示。我有 tabList object 来自 reducer(hard-coded 值)。单击一个选项卡会生成一个操作,该操作将 "state" 设置为单击的选项卡 object(我称之为 "activeTab")。

在呈现 tabList(所有选项卡)时我正在检查是否 "rendered_tabid == active_tabid" 然后添加 class "active"(这是条件基础)

active-tab-reducer.js

export const tabReducer = () => {
    return [
      {
        id: 1,
        name:"Dental Page",
        url: '#dentalPage'
      },
      {
        id: 2,
        name:"Vision Page",
        url: '#visionPage'
      },
      {
        id: 3,
        name:"Other page Tab",
        url: '#OtherPage'
      }
    ]
}


const activeTabReducer = (state = {}, action) => {
  switch(action.type) {
    case "TAB_SELECTED": return action.payload;
      break;
  }
  return state;
}

export default activeTabReducer;

(combined-reducer) index.js

import {combineReducers} from 'redux';
import activeTabReducer, {tabReducer} from './active-tab-reducer';

const allReducers = combineReducers({
  tabList: tabReducer,
  activeTab: activeTabReducer
});

export default allReducers;

(动作)index.js

export const selectTab = (tab) => {
  console.log('action invoked', tab);
  return {
    type: "TAB_SELECTED",
    payload: tab
  }
} 

tablist.js

import React from 'react';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
import {selectTab} from '../actions/index';
import TabsListItem from './tabsListItem';

class TabList extends React.Component {
  constructor(props){
    super(props);
  }

  createTabItems(){    
    return this.props.tabList.map((item, i) => {
      return (
        <TabsListItem key={i} tabList={item} /> 
      )
    });
  }

  render() {

    return (
      <div id="layout-header" className="layout-header">
        <div id="header" className="header">
          <ul className="tabs tabs--horizontal">                  
            {this.createTabItems()}    
          </ul>
        </div>
      </div>
    );
  }
};

function mapStateToProps(state) { 
  return {
    tabList: state.tabList,
    activeTab: state.activeTab      
  }
}
function matchDispatchToProps(dispatch){
  return bindActionCreators({selectTab: selectTab}, dispatch);
}
export default connect(mapStateToProps, matchDispatchToProps)(TabList);

tabListItem.js

import React from 'react';
class  TabListItem extends React.Component {
  constructor(props){
    super(props);
    console.log(this.props.tabList)
  }

  render() {   
    return (
      <li onClick={() => this.props.selectTab(this.props.tabList)}
          className={"tab  "+((this.props.activeTab.id==item.id)?'active':'')+""+( (this.props.activeTab.id==undefined) && (item.id == 1)?'active':'' ) 
          role="presentation" className={"tab  " }>
          <a href="#"> 
            <div className="tab__label">   
              <div className="tab__label__value">{this.props.tabList.name}</div>
            </div>
          </a>
      </li>        
    );
  }
};
export default TabListItem;

当我单击任何选项卡(来自 tabListItem)时,应该调度一个动作 TAB_SELECTED 动作,它使用 "activeTab" object.

设置状态

如何从 child 生成动作?

您应该通过 props 将函数传递给子组件。

由于该操作有一个 select 正确选项卡的参数,您可以使用返回函数的函数:

createTabItems() {    
 return this.props.tabList.map((item, i) => {
   return (
     <TabsListItem key={i} tabList={item} onSelect={() => this.onSelect(i).bind(this)} /> 
   );
});

}

通过这种方式,您的子组件会调用您的方法 onSelect 并传递正确的参数。

在父(容器)组件的 onSelect 方法中,您将调度您的操作:

onSelect(i) {
  this.props.selectTab(i);
}