我正在研究react-redux中间..但我不知道出了什么问题 在这个项目上
hera我在搜索栏中创建了获取汽车详细信息的文件。文件创建为“ search.js” ...您可以在此处看到。
search.js
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { getCars } from '../actions';
import { bindActionCreators } from 'redux';
class Search extends Component{
constructor(props){
super(props);
this.state = {
keyword:''
}
}
searchCars = (event) => {
event.preventDefault();
this.props.getCars(this.state.keyword)
}
handleChange = (event) => {
this.setState({
keyword:event.target.value
})
}
componentDidMount(){
console.log(this.state);
}
render(){
return(
<div className="main_search">
<form onSubmit={this.searchCars}>
<input type="text" value={this.state.keyword} onChange = {this.handleChange} />
</form>
</div>
)
}
}
// mapStateToProps
// mapDispatchToProps
function mapDispatchToProps(dispatch){
return bindActionCreators({getCars}, dispatch)
}
export default connect(null,mapDispatchToProps)(Search);
我认为错误是关于 getCars ..的错误,它在下面被描述为s'index.js'...您可以在这里
index.js
const URL_ROOT = 'http://localhost:3004'
export default function getCars(keywords){
const request = fetch(`${URL_ROOT}/carsIndex?q=${keywords}`,
{method:'GET'})
.then(response => response.json())
return{
type:'SEARCH_CARS',
payload:request
}
}
所以尝试修复它并帮助我...
答案 0 :(得分:0)
请将您的mapDispatchToProps方法更改为
const mapDispatchToProps = (dispatch)=> (
bindActionCreators(getCars, dispatch)
)