Javascript 在react视图中调用Redux操作
我试图将一个变量传递给Redux操作,但该操作正在接收未定义的,我做错了什么 index.js-我在哪里调用操作Javascript 在react视图中调用Redux操作,javascript,node.js,reactjs,redux,Javascript,Node.js,Reactjs,Redux,我试图将一个变量传递给Redux操作,但该操作正在接收未定义的,我做错了什么 index.js-我在哪里调用操作 import React from 'react' import { connect } from 'react-redux' import * as documentActions from '../../services/redux/actions/document' function Document(props) { return ( <ul
import React from 'react'
import { connect } from 'react-redux'
import * as documentActions from '../../services/redux/actions/document'
function Document(props) {
return (
<ul className="list-group group">
<li className="list-group-item group-item"
name = 'group'
onClick={() => props.toggleDoc(1)}>
{props.data.name}
</li>
</ul>
)
}
const mapStateToProps = state => {
return {
selectedId: state.selectedId
}
}
const mapDispatchToProps = dispatch => {
return {
toggleDoc: () => dispatch(documentActions.toggleDoc())
}
}
export default connect(
mapStateToProps,
mapDispatchToProps
)(Document)
减速器
import * as types from '../types'
const initialState = {
selectedId: null
}
export default function toggleDoc(state = initialState, action) {
console.log('action')
console.log(action.payload)
switch (action.type) {
case types.TOGGLE_DOC:
return {
...initialState,
selectedId: action.payload
}
default:
return state
}
}
打印内容:应该打印什么:
行动
1
道具。toggleDoc
在mapDispatchToProps
中定义。您已将其定义为:
toggleDoc:()=>dispatch(documentActions.toggleDoc())
此定义不接受任何参数,即使documentActions.toggleDoc
可以。您需要更改定义以传递该参数:
toggleDoc:(id)=>dispatch(documentActions.toggleDoc(id))
道具。toggleDoc
在mapDispatchToProps
中定义。您已将其定义为:
toggleDoc:()=>dispatch(documentActions.toggleDoc())
此定义不接受任何参数,即使documentActions.toggleDoc
可以。您需要更改定义以传递该参数:
toggleDoc:(id)=>dispatch(documentActions.toggleDoc(id))
import * as types from '../types'
const initialState = {
selectedId: null
}
export default function toggleDoc(state = initialState, action) {
console.log('action')
console.log(action.payload)
switch (action.type) {
case types.TOGGLE_DOC:
return {
...initialState,
selectedId: action.payload
}
default:
return state
}
}