我想在我的组件中触发一个 Action 。这是一个演示组件,不需要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>
                );
            }
        })
    }
)

action-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

Prop 不会包含 Action ,因为我没有将其绑定(bind)到 Prop 。我尝试在 App.js 中编写mapStateToProps和mapDispatchToProps,如下所示:

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

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

导出默认的connect(mapStateToProps,mapDispatchToProps)(MainLayout)
我正在调度不是函数错误。错误语句实际上听起来像是stackoverflow中的重复问题,但我也想知道为什么我使用mapDispatchToProps的目的也是正确的。提前致谢。

最佳答案

因此,据我了解,您正在使用export default connect(mapStateToProps, mapDispatchToProps)(MainLayout)并尝试在dispatch中调用MainLayout

因此,如果您看到react-redux文档,则可以通过以下方式调用connect:

  • connect()(//Component)->调度将作为 Prop
  • 传递
  • connect(mapStateToProps)(//Component)->调度将作为 Prop
  • 传递
  • connect(mapStateToProps,mapDispatchToProps)(//Component)-> 调度不会作为 Prop 传递到组件

  • 在方案3中,行为是这样的,因为mapDispatchToProps已经可以使用调度功能。因此,您可以将函数绑定(bind)到mapDispatchToProps中进行分派(dispatch),然后将此函数传递给您的组件。

    示例

    您要分派(dispatch)的函数是dispatchMe()dispatchMe2(),它们调用一个称为dispatchAction的操作。因此,您的mapDispatchToProps将类似于:-
    function mapDispatchToProps(dispatch){
      return {
         dispatchMe : () => {
           dispatch(dispatchAction())
        },
        dispatchMe2 : () => {
          dispatch(dispatchAction2())
        }
      }
    }
    

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

    您可以阅读here以获得更多有关此的信息

    关于reactjs - 如何将 Action 传递给Redux中的组件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40733822/

    10-12 05:27