简体   繁体   English

将变量传递给 mapDispatchToProps 中的操作

[英]Pass variable to action in mapDispatchToProps

import React from "react"
import {connect} from "react-redux"
import {increment} from "./redux"

function App(props) {    
    return (
        <div>
            <h1>{props.count}</h1>
            <button onClick={()=>props.handleClick(5)}>+</button>
        </div>
    )
    let mapDispatchToProps=(dispatch)=>
    {
        handleClick:(who)=>dispatch({
            type: "INCREMENT",
            count:who
        })    
    }
}
export default connect(state => ({count: state}),mapDispatchToProps)(App)




import redux, {createStore} from "redux"

export function increment(count) {
    return {
        type: "INCREMENT",
        count
    }
}

export function decrement() {
    return {
        type: "DECREMENT"
    }
}

function reducer(count = 0, action) {
    switch(action.type) {
        case "INCREMENT":
            return count + 1
        case "DECREMENT":
            return count - 1
        default:
            return count
    }
}

const store = createStore(reducer)
store.subscribe(() => console.log(store.getState()))
export default store

Error found: ReferenceError: mapDispatchToProps is not defined (/App.js:25)发现错误:ReferenceError:未定义 mapDispatchToProps (/App.js:25)

How do I solve this problem?我该如何解决这个问题? I am much confused about it..... I need to increment the value as depending on the value being passed please help me out of this problem.我对此很困惑.....我需要根据传递的值增加值,请帮助我解决这个问题。

The problem is you wrap your mapDispatchToProps function in the App component.问题是您将 mapDispatchToProps function 包装在 App 组件中。

function App(props) {    
    return (
        <div>
            <h1>{props.count}</h1>
            <button onClick={()=>props.handleClick(5)}>+</button>
        </div>
    )
}

let mapDispatchToProps=(dispatch)=>
    ({
        handleClick:(who)=>dispatch(increment(who))    
    })

export default connect(state => ({count: state}),mapDispatchToProps)(App)

And I have a suggestion on your function creator and reducer.我对您的 function 创建器和减速器有一个建议。 From what I see, you want to increase the value depends on your payload, not only count + 1. You can do as following.据我所知,您想要增加的值取决于您的有效负载,而不仅仅是 count + 1。您可以执行以下操作。

// Increment function creator
export function increment(count) {
    return {
        type: 'INCREMENT',
        payload: count
    }
}

// Reducer
function reducer(count = 0, action) {
    switch(action.type) {
        case: 'INCREMENT':
          return count + action.payload
        case: 'DECREMENT':
          return count - 1
        default: return count
    }      
}

Hope this help, I am new to react too, happy learning.希望对您有所帮助,我也是新手,学习愉快。

lets assume we are using Container Presentational Component假设我们正在使用Container Presentational Component

// container.js
import { connect } from "react-redux";
import { Login } from "../components/login/login.component";
import { loginUser } from "../modules/user/user.actions";

const toState = undefined;

const toDispatch = {
    login: loginUser,
};

export default connect(toState, toDispatch)(Login);

the presentational component looks some like:演示组件看起来像:


export class Login extends Component {
...
   onSubmit (event) {
     const { login } = this.props;

     const { name: pass } = this.state;
     login({ name: pass });
   }
...
}

and the loginUser action creator looks like this: loginUser 操作创建者如下所示:

export function loginUser (credentials) {
  return { type: "Login/user", payload: credentials }
}

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

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