【发布时间】:2018-03-06 15:57:25
【问题描述】:
我正在尝试将输入字段中的值推送到 firebase 应用程序。我已经设法从中获取数据,但我似乎无法理解提交功能。我觉得我错过了一些非常简单的东西。当我尝试使用 savePost 时,它说它不是一个函数。
the action is:
export function savePost(post) {
return dispatch => database.push(post)
}
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { savePost } from '../actions/index';
class AddPost extends Component {
constructor(props) {
super(props);
this.state= {
title: '',
body: ''
}
this.onChangeTitle = this.onChangeTitle.bind(this)
this.onChangeBody = this.onChangeBody.bind(this)
this.onSubmit = this.onSubmit.bind(this)
}
onChangeTitle(e){
this.setState({
title: e.target.value,
})
}
onChangeBody(e){
this.setState({
body: e.target.value,
})
}
onSubmit(e, values) {
e.preventDefault();
savePost(this.state)
}
render() {
return (
<div className="container">
<form onSubmit={this.onSubmit.bind(this)}>
<input value={this.state.title} onChange={this.onChangeTitle} type="text" name="title" />
<input value={this.state.body} onChange={this.onChangeBody} type="text" name="body"/>
<button type="submit">Post</button>
</form>
</div>
);
}
}
const mapDispatchToProps = dispatch => ({
savePost: dispatch(savePost())
});
export default connect(undefined, mapDispatchToProps)(AddPost);
【问题讨论】:
-
你能发布你的
configureStore.js或store.js的样子吗? -
不需要是
this.props.savePost -
const createStoreWithMiddleware = applyMiddleware(thunk)(createStore) ReactDOM.render(
, document.getElementById('root') ) registerServiceWorker()
标签: reactjs firebase redux firebase-authentication