javascript - React does not recognize my function -
i developing application in reactjs, utilizing redux architecture. application using third-party api gets called when submit city search bar. in testing it, console error reads:
uncaught typeerror: this.props.fetchweather not function
i unclear why react tells me fetchweather not function when exists function here on src/actions/index.js:
import axios '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); need fetch weather data , line of code exists in container/search_bar.js:
import react, {component} 'react'; import {connect} 'react-redux'; import {bindactioncreators} 'redux'; import {fetchweather} '../actions/index'; export default class searchbar extends component { constructor(props) { super(props); this.state = {term: ''}; this.oninputchange = this.oninputchange.bind(this); // need bind this.onformsubmit = this.onformsubmit.bind(this); } oninputchange(event) { this.setstate({term: event.target.value}); } // callback onformsubmit(event) { event.preventdefault(); // need go fetch weather data this.props.fetchweather(this.state.term); // clear out search input this.setstate({term:''}); } render() { return ( <form onsubmit={this.onformsubmit} classname="input-group"> <input placeholder="get 5 day forecast in 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 flows down middleware return bindactioncreators({fetchweather}, dispatch); } connect(null, mapdispatchtoprops)(searchbar);
you need export redux component, not react component
export default connect(null, mapdispatchtoprops)(searchbar);
Comments
Post a Comment