如何将操作传递给redux中的组件

时间:2016-11-22 04:33:13

标签: reactjs redux react-redux jsx react-router-redux

我想在我的组件中触发一个动作。这是一个演示组件,不需要知道redux。路由器实现使用react-router-redux完成。

main.js:

let store = createStore(rootReducer)

const history = syncHistoryWithStore(hashHistory, store)

class RenderClass extends Component {
  render() {
    return (
        <Provider store={store}>
            <Router history={history}>
                <Route path="/" component={MainLayout}>
                    <Route name="employees" path="/employees" url="http://localhost:8080/" component={EmployeeTable}></Route>
                    <Route name="personalInformation" path="/personalInformation/:employeeId" url={URI} component={PersonalInformation} />
                    .....
                </Route>
            <Router>
        </Provider>
        );
    }
}   

App.jsx:

import * as Actions from './action-creator';
const MainLayout = React.createClass({
                render: function() {

                    const { dispatch, list } = this.props;
                    let actions = bindActionCreators(Actions, dispatch);
                    console.log(actions);

                    return (
                        <div className="wrapper">
                            <Header actions={actions} list={list} params={this.props.params} />

                    {React.cloneElement(this.props.children, this.props)}
                        </div>
                    )
                }
            }); 

function select(state) {
   return {
      list: state.listingReducer
   }
}
export default connect(select)(MainLayout);

header.js:

define(
    [
        'react',
        'jquery',
        'appMin'
    ],
    function (React, $) {
        var Link = require('reactRouter').Link;
        var Header = React.createClass({
            handleClick: function () {
                //Action called here
                this.props.actions.listEmployees();
            },      
            render: function () {
                return (
                    <ul className="sidebar-menu">
                        <li>
                            <Link to={'/employees'} onClick={this.handleClick}><i className="fa fa-home"></i>Employees</Link> 
                        </li> 
                    </ul>
                );
            }
        });
        return Header;
    }
)

employee.js:

define(
    [
        'react',
        'jquery',
        'appMin'
    ],
    function (React, $) {
        var EmployeeTable = React.createClass({

            render: function () {
                if (this.props.list != undefined) {
                    var listItems = this.props.list.map(function (listItem) {
                        return (
                            <tr key={listItem.id}>
                                <td> {listItem.name}</td>
                <td><Link to={'/personalInformation/' + employee.id} onClick={this.props.actions.displayPersonalInformation(employee.id)}>Personal Information</Link></td>
                                ......
                            </tr>
                        );
                    }, this);   
                    })
                }
                return (
                    <table>
                        <tbody>
                            {listItems}
                        </tbody>                    
                    </table>
                );
            }
        })  
    }
)

动作creator.js:

export function listEmployees() {
    return {
      type: types.LIST_EMPLOYEES
   };
}

export function displayPersonalInformation() {
        return {
          type: types.DISPLAY_PERSONAL_INFORMATION
       };
    }

reducer.js:
function addEmployee(state, action) {

   switch (action.type) {

      case types.LIST_EMPLOYEES:
         return {"id":"1", "name":"Stackoverflow"}

      default:
        return state
   }
}

function listingReducer(state = [], action) {
    switch (action.type) {

        case types.LIST_EMPLOYEES:
            return [
                ...state, 
                addEmployee(undefined, action)
            ]

        case types.DISPLAY_PERSONAL_INFORMATION:
                return // Gets personal Information

        default:
            return state;
    }
}


const rootReducer = combineReducers({
   listingReducer
})

export default rootReducer

道具不包含动作,因为我没有把它绑在道具上。我尝试在 App.js 中编写mapStateToProps和mapDispatchToProps,如下所示:

function mapStateToProps(state) {
   return {
        list: state.list
    }
}

function mapDispatchToProps(dispatch) {
   return { actions: bindActionCreators({ actionCreators }, dispatch) }
}

export default connect(mapStateToProps,mapDispatchToProps)(MainLayout) 我收到调度不是函数错误。错误语句实际上听起来像stackoverflow中的重复问题,但我也想知道我使用mapDispatchToProps的目的也是正确的。提前谢谢。

5 个答案:

答案 0 :(得分:5)

根据我的理解,您使用export default connect(mapStateToProps, mapDispatchToProps)(MainLayout)并尝试在dispatch中致电MainLayout

因此,如果您发现可以通过以下方式呼叫连接,请参阅react-redux文档: -

  • connect()(//Component) - &gt; dispatch将作为道具传递
  • connect(mapStateToProps)(//Component) - &gt; dispatch将作为道具传递
  • connect(mapStateToProps,mapDispatchToProps)(//Component) - &gt; 调度不会作为道具传递给您的组件

在方案3中,行为是这样的,因为mapDispatchToProps已经可以访问调度功能。因此,您可以将函数绑定到mapDispatchToProps中的调度,然后将此函数传递给组件。

示例

您要发送的功能是dispatchMe()dispatchMe2(),它会调用名为dispatchAction的操作。所以你的mapDispatchToProps会是这样的: -

function mapDispatchToProps(dispatch){
  return {
     dispatchMe : () => {
       dispatch(dispatchAction()) 
    },
    dispatchMe2 : () => {
      dispatch(dispatchAction2())
    }
  }
}

现在,在您的组件中,您可以传递dispatchMe并在需要时调用它。

您可以阅读here了解有关此内容的更多信息

答案 1 :(得分:0)

由于您已经使用了bindActionCreatorsreduxdispatch在您调用动作创建者时displayPersonalInformation执行此操作。

正如@Harkirat Saluja在回答中提到的,如果您使用了mapDispatchToProps,那么您的组件将不会dispatch函数与props绑定。

但是如果你想dispatch props(不是最佳做法),那么你可以拥有类似的东西,

function mapDispatchToProps(dispatch) {
   return { dispatch, ...actions: bindActionCreators({ actionCreators }, dispatch) }
}

答案 2 :(得分:0)

  class MainLayout extends React.component{
      render(){
        console.log(this.props.list);
        return(
            <div>
               <h1>mainlayout</h1>
             </div>
            );
        } 
    }
  function mapStateToProps(state) {
       return {
           list: state.listingReducer
       }
   }
   export default connect(mapStateToProps)(MainLayout);

答案 3 :(得分:0)

您无法通过道具访问动作,因为您正在将动作分配给动作属性。将动作绑定到道具的方式可以通过this.props.actions.myFunction访问函数。如果您想直接通过道具调用动作,则将动作与道具连接起来,如下所示:

const mapDispatchToProps = (dispatch) => {
    return bindActionCreators(actionCreators, dispatch);
};

export default connect(null, mapDispatchToProps)(MainLayout);

答案 4 :(得分:0)

import { bindActionCreators, } from 'redux';
import { connect, } from 'react-redux';

/**
 * Map redux actions to the Component's props
 * @param {*} dispatch The Redux store's dispatch method
 * @returns {*} Object
 */
const mapDispatchToProps = (dispatch) => {
    return bindActionCreators({
        yourActions,
    }, dispatch);
};

export default connect(mapDispatchToProps)(MainLayout);