没有触发 react redux 调度功能
react redux dispatch fuction is not triggered
i attach all my required files below for references
when i try to call react redux dispatch fuction is not triggered. am new to react redux
action.js 文件
import { ITS_USER } from './types';
import {store} from './index'
export function addPerson(person) {
console.log("chek",person);
console.log("state",store.getState());
return dispatch => {
dispatch(adduser(person));
}
}
export function adduser(person){
return {
type: ITS_USER,
payload: person
};
}
Reducer.js
import { ITS_USER } from './types';
export default function(state=[], action) {
console.log("dxasdads",action.type)
switch (action.type) {
case ITS_USER:
return action.payload;
default:
return state;
}
}
触发函数调用
**var person="praveen"
addPerson(person)**
您必须将您的组件与 react-redux
库提供的 connect
HOC 连接,然后您可以通过从作为第一个参数传递给连接的 mapStateToProps
调度它来触发调度函数功能。像这样:
import YOUR_COMPONENT from './PATH_TO_YOUR_COMPONENT'
const mapDispatchToProps = dispatch => ({
addPerson: (person) => dispatch(addPerson(person)),
});
export default connect(null, mapDispatchToProps)(YOUR_COMPONENT);
然后在您的组件中,您将获得 addPerson
作为组件 props
的功能。您可以简单地通过 this.props.addPerson(person)
来调用它。
i attach all my required files below for references when i try to call react redux dispatch fuction is not triggered. am new to react redux
action.js 文件
import { ITS_USER } from './types';
import {store} from './index'
export function addPerson(person) {
console.log("chek",person);
console.log("state",store.getState());
return dispatch => {
dispatch(adduser(person));
}
}
export function adduser(person){
return {
type: ITS_USER,
payload: person
};
}
Reducer.js
import { ITS_USER } from './types';
export default function(state=[], action) {
console.log("dxasdads",action.type)
switch (action.type) {
case ITS_USER:
return action.payload;
default:
return state;
}
}
触发函数调用
**var person="praveen"
addPerson(person)**
您必须将您的组件与 react-redux
库提供的 connect
HOC 连接,然后您可以通过从作为第一个参数传递给连接的 mapStateToProps
调度它来触发调度函数功能。像这样:
import YOUR_COMPONENT from './PATH_TO_YOUR_COMPONENT'
const mapDispatchToProps = dispatch => ({
addPerson: (person) => dispatch(addPerson(person)),
});
export default connect(null, mapDispatchToProps)(YOUR_COMPONENT);
然后在您的组件中,您将获得 addPerson
作为组件 props
的功能。您可以简单地通过 this.props.addPerson(person)
来调用它。