【发布时间】:2017-12-05 13:28:38
【问题描述】:
我收到此错误:this.props.postBooks 不是函数。
我有一个动作 - postBooks - 我试图通过道具发送。
这是我的组件:
"use strict"
import React from 'react'
import {Well,Panel,FormControl,FormGroup,ControlLabel,Button} from 'react-bootstrap'
import {connect} from 'react-redux'
import {bindActionCreators} from 'redux'
import {postBooks} from '../../actions/booksActions'
import {findDOMNode} from 'react-dom'
export class BooksForm extends React.Component{
handleSubmit(){
const book = [{
title: findDOMNode(this.refs.title).value,
description: findDOMNode(this.refs.description).value,
price: findDOMNode(this.refs.price).value
}]
this.props.postBooks(book)
}
render(){
return(
<Well>
<Panel>
<FormGroup controlId='title'>
<ControlLabel> Title </ControlLabel>
<FormControl
type='text'
placeholder='Enter Title'
ref='title' />
</FormGroup>
<FormGroup controlId='description'>
<ControlLabel> Enter Description </ControlLabel>
<FormControl
type='text'
placeholder='Enter Description'
ref='description' />
</FormGroup>
<FormGroup controlId='price'>
<ControlLabel> Enter Price </ControlLabel>
<FormControl
type='text'
placeholder='Enter Price'
ref='price' />
</FormGroup>
<Button
onClick={this.handleSubmit.bind(this)}
bsStyle='primary'> Enter New Book </Button>
</Panel>
</Well>
)
}
}
function mapDispatchToProps(dispatch){
return bindActionCreators({postBooks},dispatch)
}
export default connect(null,mapDispatchToProps)(BooksForm);
似乎调度没有按预期映射到道具,因为在控制台记录道具时,道具是空的。任何帮助表示赞赏。提前致谢
编辑:添加操作
"use strict"
// POST A BOOK
export function postBooks(book){
return {
type:"POST_BOOK",
payload: book
}
}
// DELETE A BOOK
export function deleteBooks(id){
return {
type:"DELETE_BOOK",
payload: id
}
}
//UPDATE BOOK
export function updateBooks(book){
return {
type:"UPDATE_BOOK",
payload: book
}
}
//Retrieve all books as if using API
export function getBooks(){
return{
type:'GET_BOOKS'
}
}
【问题讨论】:
-
我们可以看到
mapDispatchToProps代码吗? -
如果我理解正确的话,mapDispatchToProps 函数位于组件代码的底部。
标签: reactjs redux react-redux