将道具从孩子传递到父母(无上下文)

OunknownO

将状态传递给父母时,我的头有些wrapping绕。我需要将数据从表单容器发送到应用程序,以便在提交后可以在天气信息中显示列表的更新状态

class App extends Component {

render() {
return (
  <div className="App">
    <div className="App-header">
      <img src={logo} className="App-logo" alt="logo" />
      <h2>Weather App</h2>
    </div>
    <p className="App-intro">
      To get started, edit <code>src/App.js</code> and save to reload.
    </p>
      <FormContainer label="Name of the city:"/>
      <WeatherInfo
          nameOfCity={this.state.nameOfCity}
          weatherDescription={this.state.weatherDescription}
          windSpeed={this.state.windSpeed}
          temperature={this.state.temperature}
          maxTemperature={this.state.maxTemperature}
          minTemperature={this.state.minTemperature}
      />
  </div>
);
}
}
export default App;

表格容器

class FormContainer extends Component {

constructor(props) {
    super(props);
    this.state = {
        cityName: '',
        nameOfCity:'',
        weatherDescription:'',
        windSpeed:'',
        temperature:'',
        maxTemperature:'',
        minTemperature:''
    };
    this.handleFormSubmit = this.handleFormSubmit.bind(this);
    this.handleCityName = this.handleCityName.bind(this);
}

handleFormSubmit(e) {
    e.preventDefault();

    const SendForm = {
        cityName: this.state.cityName
    };
    console.log(SendForm);
    fetch(`http://api.openweathermap.org/data/2.5/forecast/weather?q=${SendForm.cityName}&units=metric&APPID=********`)
        .then(res => res.json())
        .then(results => {
            this.setState({
                nameOfCity: results.city.name,
                weatherDescription: results.list[0].weather[0].description,
                windSpeed: results.list[2].wind.speed,
                temperature: results.list[0].main.temp,
                maxTemperature: results.list[0].main.temp_max,
                minTemperature: results.list[0].main.temp_min
            });
        });
}

handleCityName(value) {
    this.setState({ cityName: value });
}

render() {
    return (
        <div>
        <form onSubmit={this.handleFormSubmit}>
            <label>{this.props.label}</label>
            <SearchBar
                name="CityName"
                type="text"
                value={this.state.cityName}
                placeholder="search"
                onChange={this.handleCityName}
            />
            <button type="submit"
                    className=""
                    value='Submit'
                    placeholder="Search" />
        </form>

        </div>
    );
}
}

export {FormContainer};

搜索栏组件

const SearchBar = (props) => (
<div>
    <label>{props.label}</label>
    <input name={props.name} type={props.inputType} value={props.value} placeholder={props.placeholder} onChange={(e)=>props.onChange(e.target.value)}/>
</div>
);

export default SearchBar;

和天气信息组件

const WeatherInfo = (props) => (
<div>
    <ul>
        <li>{props.nameOfCity}</li>
        <li>{props.weatherDescription}</li>
        <li>{props.windSpeed}</li>
        <li>{props.temperature}</li>
        <li>{props.maxTemperature}</li>
        <li>{props.minTemperature}</li>
    </ul>
</div>
);

export default WeatherInfo;
安德鲁·帕拉莫什金

您可以传递方法以将App状态更新FormContainer组件

class App extends Component {

constructor() {
    this.state = {
        cityName: '',
        nameOfCity:'',
        weatherDescription:'',
        windSpeed:'',
        temperature:'',
        maxTemperature:'',
        minTemperature:''
    };
}

updateInfo(results) {
   this.setState({
                nameOfCity: results.city.name,
                weatherDescription: results.list[0].weather[0].description,
                windSpeed: results.list[2].wind.speed,
                temperature: results.list[0].main.temp,
                maxTemperature: results.list[0].main.temp_max,
                minTemperature: results.list[0].main.temp_min
            });
 }

render() {
return (
  <div className="App">
    <div className="App-header">
      <img src={logo} className="App-logo" alt="logo" />
      <h2>Weather App</h2>
    </div>
    <p className="App-intro">
      To get started, edit <code>src/App.js</code> and save to reload.
    </p>
      <FormContainer label="Name of the city:" updateInfo={this.updateInfo.bind(this)}
          nameOfCity={this.state.nameOfCity}
      />
      <WeatherInfo
          nameOfCity={this.state.nameOfCity}
          weatherDescription={this.state.weatherDescription}
          windSpeed={this.state.windSpeed}
          temperature={this.state.temperature}
          maxTemperature={this.state.maxTemperature}
          minTemperature={this.state.minTemperature}
      />
  </div>
);
}
}
export default App;

并从 FormComponent

class FormContainer extends Component {

constructor(props) {
    super(props);
    this.handleFormSubmit = this.handleFormSubmit.bind(this);
    this.handleCityName = this.handleCityName.bind(this);
}

handleFormSubmit(e) {
    e.preventDefault();

    const SendForm = {
        cityName: this.props.cityName
    };
    console.log(SendForm);
    fetch(`http://api.openweathermap.org/data/2.5/forecast/weather?q=${SendForm.cityName}&units=metric&APPID=********`)
        .then(res => res.json())
        .then(results => {
            this.props.updateInfo(results);
        });
}

handleCityName(value) {
    // Do what you want to do, like resend API request or smth
}

render() {
    return (
        <div>
        <form onSubmit={this.handleFormSubmit}>
            <label>{this.props.label}</label>
            <SearchBar
                name="CityName"
                type="text"
                value={this.props.cityName}
                placeholder="search"
                onChange={this.handleCityName}
            />
            <button type="submit"
                    className=""
                    value='Submit'
                    placeholder="Search" />
        </form>

        </div>
    );
}
}

export {FormContainer};

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在孩子的上下文(而不是父母的上下文)下解析父组件的道具

来自分类Dev

将多个道具传递给组件(上下文 API)

来自分类Dev

使用上下文API将数据从父级传递给孩子和同级孩子

来自分类Dev

VBA的无上下文语法

来自分类Dev

查找无上下文语法

来自分类Dev

从父母到孩子的反应传递道具行为异常

来自分类Dev

将变量从上下文传递到另一个上下文

来自分类Dev

将变量从上下文传递到另一个上下文

来自分类Dev

将“ refetch”传递给上下文

来自分类Dev

特定语言的无上下文语法

来自分类Dev

语言问题带来的无上下文语法

来自分类Dev

特定语言的无上下文语法

来自分类Dev

证明语言是无上下文的,带有抽动引理

来自分类Dev

BaseX XQuery:REPO:无上下文值绑定

来自分类Dev

给出生成以下语言的无上下文语法

来自分类Dev

将参数传递到模板而不覆盖数据上下文

来自分类Dev

如何将正确的上下文传递到新的RelativeLayout中?Android Java

来自分类Dev

哪些方法可以将QObject列表传递到QML上下文?

来自分类Dev

将值从装饰器传递到Django中的上下文处理器

来自分类Dev

Swift:将数据传递到捕获上下文的闭包

来自分类Dev

将“持久”中的上下文传递到“保存后”

来自分类Dev

如何将变量从“上下文”传递到页面查询?| 盖茨比

来自分类Dev

将腻子上下文传递到客户端剪贴板?

来自分类Dev

将ZMQ上下文从C传递到嵌入式Lua

来自分类Dev

将数据从django middleware.process_view传递到模板上下文

来自分类Dev

我该如何将道具/上下文传递给有活力的儿童以做出反应?

来自分类Dev

传递到BroadcastReceiver的onReceive()中的上下文是什么?

来自分类Dev

将上下文从JUnit Runner传递到黄瓜步骤

来自分类Dev

有什么方法可以将元素从一个上下文传递到另一个上下文?

Related 相关文章

  1. 1

    在孩子的上下文(而不是父母的上下文)下解析父组件的道具

  2. 2

    将多个道具传递给组件(上下文 API)

  3. 3

    使用上下文API将数据从父级传递给孩子和同级孩子

  4. 4

    VBA的无上下文语法

  5. 5

    查找无上下文语法

  6. 6

    从父母到孩子的反应传递道具行为异常

  7. 7

    将变量从上下文传递到另一个上下文

  8. 8

    将变量从上下文传递到另一个上下文

  9. 9

    将“ refetch”传递给上下文

  10. 10

    特定语言的无上下文语法

  11. 11

    语言问题带来的无上下文语法

  12. 12

    特定语言的无上下文语法

  13. 13

    证明语言是无上下文的,带有抽动引理

  14. 14

    BaseX XQuery:REPO:无上下文值绑定

  15. 15

    给出生成以下语言的无上下文语法

  16. 16

    将参数传递到模板而不覆盖数据上下文

  17. 17

    如何将正确的上下文传递到新的RelativeLayout中?Android Java

  18. 18

    哪些方法可以将QObject列表传递到QML上下文?

  19. 19

    将值从装饰器传递到Django中的上下文处理器

  20. 20

    Swift:将数据传递到捕获上下文的闭包

  21. 21

    将“持久”中的上下文传递到“保存后”

  22. 22

    如何将变量从“上下文”传递到页面查询?| 盖茨比

  23. 23

    将腻子上下文传递到客户端剪贴板?

  24. 24

    将ZMQ上下文从C传递到嵌入式Lua

  25. 25

    将数据从django middleware.process_view传递到模板上下文

  26. 26

    我该如何将道具/上下文传递给有活力的儿童以做出反应?

  27. 27

    传递到BroadcastReceiver的onReceive()中的上下文是什么?

  28. 28

    将上下文从JUnit Runner传递到黄瓜步骤

  29. 29

    有什么方法可以将元素从一个上下文传递到另一个上下文?

热门标签

归档