Reputation:
I am developing an application in Reactjs, utilizing the Redux architecture. The application is using a third-party api that gets called when I submit a city into the search bar. In testing it, I get a console error which reads:
Uncaught TypeError: this.props.fetchWeather is not a function
I am unclear as to why React tells me that fetchWeather is not a function when it exists as a function here on src/actions/index.js:
import axios from 'axios';
const API_KEY = 'spj3q-9huq]-hq -9hq 0rgjeth9e';
const ROOT_URL = `http://api.openweathermap.org/data/2.5/forecast?appid=${API_KEY}`;
export const FETCH_WEATHER = 'FETCH_WEATHER';
export function fetchWeather(city) {
const url = `${ROOT_URL}&q=${city},us`;
const request = axios.get(url);
return {
type: FETCH_WEATHER,
// optional property
// promise passed in
payload: request
};
}
this.props.fetchWeather(this.state.term); is what I need to fetch the weather data and this line of code exists in container/search_bar.js:
import React, {Component} from 'react';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
import {fetchWeather} from '../actions/index';
export default class SearchBar extends Component {
constructor(props) {
super(props);
this.state = {term: ''};
this.onInputChange = this.onInputChange.bind(this);
// always need to bind this
this.onFormSubmit = this.onFormSubmit.bind(this);
}
onInputChange(event) {
this.setState({term: event.target.value});
}
// callback
onFormSubmit(event) {
event.preventDefault();
// We need to go fetch weather data
this.props.fetchWeather(this.state.term);
// clear out the search input
this.setState({term:''});
}
render() {
return (
<form onSubmit={this.onFormSubmit} className="input-group">
<input
placeholder="Get a five day forecast in your favorite cities"
className="form-control"
value={this.state.term}
onChange={this.onInputChange}
/>
<span className="input-group-btn">
<button type="submit" className="btn btn-secondary">Submit</button>
</span>
</form>
)
}
}
function mapDispatchToProps(dispatch) {
// makes sure this flows down into the middleware
return bindActionCreators({fetchWeather}, dispatch);
}
connect(null, mapDispatchToProps)(SearchBar);
Upvotes: 0
Views: 769
Reputation: 15513
You need to export the redux component, not the react component
export default connect(null, mapDispatchToProps)(SearchBar);
Upvotes: 1