简体   繁体   English

将支持从孩子传给父母React

[英]Pass prop from child to parent React

In WeatherForecast component I need to pass the returned value of function appColor into a property. WeatherForecast组件中,我需要将函数appColor的返回值传递给属性。 Then the property from WeatherForecast needs to be passed into className of app component. 然后需要将WeatherForecast的属性传递到app组件的className中。 New to react. 新的反应。 Not sure how to pass property from child to component. 不确定如何将属性从子节点传递给组件。

class WeatherForecast extends Component {

  appColor(weatherData) {
    //Check for condition and return value
    return "example-color1"
  }

  render() {
    /************************************
    // Need to Pass returned value of Function into propery or variable?
    /************************************/ 
    let bgColor = appColor(weatherData);

    return (
      <div className="text-center col-xs-12">

         <h1 id="temp">{this.displayTemp(this.props.weather)}</h1>
         <h1>{this.displayCity(this.props.weather)}</h1> 

      </div>
    );
  }
}



export default class App extends Component {

  render() {
    return (
      <div className={"app-container" + this.AppColorPropertyClass}>

        <div className="main-wrapper">

            <WeatherForecast bgColorPropertyClass={this.AppColorPropertyClass} />

        </div> 

      </div>  

    );
  }
}

You can pass a function from the parent to the child, and the child can call that function with the color (pretty much operates like an event handler). 您可以将一个函数从父级传递给子级,子级可以使用该颜色调用该函数(几乎像事件处理程序一样运行)。 When the color is received back in App, assign it to a state value using .setState() which will then get picked up in render() 当在App中收到颜色时,使用.setState()将其指定为状态值,然后在render()中将其拾取

export default class App extends Component {
  constructor(props) {
    super(props);

    this.state = { appColorClass: 'some-default-color' };
  }

  setAppColor(colorClass) {
    this.setState({ appColorClass: colorClass });
  }

  render() {
    return (
      <div className={"app-container" + this.state.appColorClass}>

        <div className="main-wrapper">

          <WeatherForecast getBgColorPropertyClass={ color => this.setAppColor(color) } />

        </div>

      </div>

    );
  }
}


class WeatherForecast extends Component {
  componentWillMount() {
    if (this.props.getBgColorPropertyClass) {
      // TODO: Get "weatherData" from somewhere (maybe from this.props.weather ??)
      this.props.getBgColorPropertyClass(this.appColor(weatherData));
    }
  }

  appColor(weatherData) {
    //Check for condition and return value
    return "example-color1"
  }

  render() {
    return (
      <div className="text-center col-xs-12">

         <h1 id="temp">{this.displayTemp(this.props.weather)}</h1>
         <h1>{this.displayCity(this.props.weather)}</h1> 

      </div>
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 React 中将整数值从父道具传递给子道具 - How to pass integer value from parent prop to child prop in React 使用 React Hooks,当我将 prop 从父组件传递给子组件时,子组件中的 prop 未定义 - Using React Hooks, when I pass down a prop from parent to a child component, the prop in the child component is undefined 如何在Vuejs中将父母的道具传给孩子 - How to pass a prop from parent to child in Vuejs 如何将 React 道具从父母传递给孩子,再传递给另一个孩子? - How do I pass a React prop from Parent to Child, to another Child? 将道具从孩子传递给父母不工作(反应) - Passed prop from child to parent not working (React) 使用React和Gatsby将道具从父母传递给孩子 - Passing a prop to a child from parent with React and Gatsby 反应从孩子到父母的更新数组道具 - React update array prop from child to parent 在 REACT.js 中,如何将状态从子组件传递到父组件作为道具 - In REACT.js how can I pass a state from child component up to Parent component as a prop React Native:如何将参数从子组件道具传递给父组件 - React Native: How to pass params to parent component from child component prop React-Router-Dom v5.2,无法将道具从父级收到的道具传递给子组件 - React-Router-Dom v5.2 , unable to pass props to a child component from prop recieved by parent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM