2017-10-05 21 views
0

SearchInput(親)からFetchData(子)コンポーネントに値を渡したいとします。データをフェッチするために2回クリックする必要があるため、送信ボタンをクリックしてthis.props.loadedをtrueにする必要があるため、正常に動作しません。私はコールバック関数を使うべきだと知っていますが、私は知っていません。私は1週間前にReactJSを学び始めました。クリック送信ボタンで値を子コンポーネントに渡す - ReactJSを2回クリックします

import React, {Component} from "react"; 
 

 
import FetchData from "./FetchData"; 
 

 
export default class SearchInput extends Component { 
 
    constructor(props) { 
 
    super(props); 
 
    this.state = { 
 
     cityName: "", 
 
     loaded: false 
 
    } 
 
    this.handleCityNameChange = this 
 
     .handleCityNameChange 
 
     .bind(this); 
 
    this.handleSubmitButton = this 
 
     .handleSubmitButton 
 
     .bind(this); 
 
    } 
 
    handleCityNameChange = (e) => { 
 
    const val = e.target.value; 
 
    this.setState({cityName: val}); 
 
    e.preventDefault(); 
 
    } 
 
    handleSubmitButton = (e) => { 
 
    //const val = document.getElementById("search").value; 
 
    this.setState({cityName: this.state.cityName, loaded: true}); 
 
    e.preventDefault(); 
 
    } 
 
    render() { 
 
    const {cityName, loaded} = this.state; 
 
    return (
 
     <div> 
 
     <form> 
 
      <label htmlFor="search">Search city:</label> 
 
      <input 
 
      type="text" 
 
      name="search" 
 
      id="search" 
 
      value={this.state.cityName} 
 
      onChange={this.handleCityNameChange}/> 
 
      <input type="submit" onClick={this.handleSubmitButton}/> 
 
     </form> 
 
     <FetchData 
 
      cityName={cityName} 
 
      loaded={loaded} 
 
      handleSubmitButton={this.handleSubmitButton}/> 
 
     </div> 
 
    ) 
 
    } 
 
} 
 

 

 
import React, {Component} from "react"; 
 
import axios from "axios"; 
 

 
import DisplayWeather from "./DisplayWeather"; 
 

 
export default class FetchData extends Component { 
 
    constructor(props) { 
 
     super(props); 
 
     this.state = { 
 
      descriptionMain: "", 
 
      description: "", 
 
      temperature: null, 
 
      weatherIcon: "" 
 
     } 
 
     
 
    } 
 
    // otherFunc() { 
 
    //  this.props.handleSubmitButton(); 
 
    // } 
 
    fetchData =() => { 
 
     if (this.props.loaded) { 
 
      const apiURL = `https://api.openweathermap.org/data/2.5/weather?q=${this.props.cityName}&units=metric&APPID=e6f4d816d3ade705ec1d8d9701b61e14`; 
 
      console.log(apiURL) 
 
      axios 
 
       .get(apiURL) 
 
       .then(res => { 
 
        this.setState({descriptionMain: res.data.weather[0].main, description: res.data.weather[0].description, temperature: res.data.main.temp, weatherIcon: res.data.weather[0].icon}); 
 
       }) 
 
     } 
 
    } 
 
    componentWillReceiveProps() { 
 
     this.fetchData(); 
 
    } 
 
    render() { 
 
     return (
 
      <div> 
 
       <DisplayWeather {...this.state} cityName={this.props.cityName}/> 
 
      </div> 
 
     ) 
 
    } 
 
} 
 

 

 
export default class DisplayWeather extends Component { 
 
    render() { 
 
    const {descriptionMain, description, temperature, weatherIcon, cityName} = this.props; 
 
    return (
 
     <div> 
 
     <h3>{cityName}</h3> 
 
     <h4>Sky: {description}</h4> 
 
     <h5>Description: {descriptionMain}</h5> 
 
     <span className="temperature">{temperature} 
 
      °C</span> 
 
     <img 
 
      src={`http://openweathermap.org/img/w/${weatherIcon}.png`} 
 
      alt={`${description}`}/> 
 
     </div> 
 
    ) 
 
    } 
 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

答えて

0

あなたは矢印の機能に提出した属性を囲む必要があります:

onClick{()=>this.handleSubmutButton()} 

も同様に変更のため

+0

を同じ操作を行い、私はこれにSeachInput入力を変更し、それは動作しません(undefinedのプロパティターゲットを読み取ることはできません - handleCityNameChange)。 '<入力 タイプ= "テキスト" 名= ID = "検索" "検索" 値= {this.state.cityName} のonChange = {()=> this.handleCityNameChange()} /> – Natalia

+0

要素をパラメータに渡す – akiespenc

+0

これはまだ動作しません。初めてデータを表示するには2回クリックする必要があります。 2回目にsthと入力すると、Uncaught(約束しています)エラーが発生しました。エラー:APIが既にロードされていて、onChangeでhandleCityNameChangeが発生したため、リクエストがステータスコード404-で失敗しました。 '<入力 タイプ= "テキスト" 名= ID = "検索" "検索" 値= {this.state.cityName} のonChange = {(E)=> this.handleCityNameChange(E)} /> this.handleSubmitButton(e)} /> ' – Natalia

関連する問題