我應該通過在mapDispatchToProps參數(在地方「????????」是)Redux mapDispatch ToProps。我應該通過哪些數據作爲參數?
我的行爲是在這裏:
function addTodo(text) {
return {
type: 'ADD_TODO',
text: text
}
}
export default addTodo
這是我的組件/容器這裏我使用mapDispachToProps(){}和mapStateToProps(){}
import React, {Component} from 'react';
import addTodo from '../actions/index';
import {connect} from 'react-redux';
class TodoInput extends Component {
constructor(props) {
super(props)
this.state = {
inputValue: ''
}
}
onChangeHandle(event) {
this.setState({
inputValue: event.target.value
})
}
handleSubmit(event) {
event.preventDefault()
//this.props.dispatch(addTodo(this.state.inputValue))
this.props.buttonClick(this.state.inputValue)
}
render() {
return (
<div>
<input
type = "text"
value = {this.state.inputValue}
onChange = {this.onChangeHandle.bind(this)}
/>
<button onClick={this.handleSubmit.bind(this)}>Submit</button>
</div>
);
}
}
const mapStateToProps = (state) => {
return state
}
const mapDispatchToProps = (dispatch) => {
return {
buttonClick:() => dispatch(addTodo(???????????))
}
}
export default connect(mapStateToProps, mapDispatchToProps)(TodoInput);
這麼簡單:)謝謝:) – Lukas
最歡迎的。 –