当前位置: 首页 > 面试题库 >

将道具从孩子传递给父母React

邢英奕
2023-03-14
问题内容

WeatherForecast组件中,我需要将函数的返回值传递appColor给属性。然后从属性WeatherForecast需要被传递到classNameapp组件。新来的反应。不确定如何将属性从子级传递到组件。

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>

    );
  }
}

问题答案:

您可以将函数从父级传递给子级,子级可以使用颜色调用该函数(很多操作都类似于事件处理程序)。在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>
    );
  }
}


 类似资料:
  • 问题内容: 我在这里有一个简单的设置: 我有一个父组件,该父组件有2个子组件。在第一个子组件中:用户更改输入的值。然后,该更改的值将是我想要从该孩子传递给父对象的道具,以便可以将其传递给与同一父组件相连的另一个孩子。 当前设置的这个,请查看 从用户输入到UI更改的流程:1.在“ Child 1”中:调整一个滑块,将onChange值传递给父组件;2.将此属性(新的滑块值)传递给Parent组件,以

  • 我想在React中将数据从子组件发送到父组件。查找数据时,只有类类型,没有功能文档。您可以使用什么语法传递数据?

  • 问题内容: 我想创建一个应用程序,其中应处理某些事件,就像它们被传递到父容器一样。例如,我有一个包含的。顶部立即实现了鼠标按下和拖动。为了使事件看起来像是到达的,而不是标签本身,我需要做什么。(更改源对象很重要) 是否有比实际实现事件并在父级中复制事件更好的解决方案?(在某些对象> 5个孩子之后,这将变得乏味)。 问题答案: 在事件侦听器中,您可以将事件调度到父组件。 作为事件处理函数参数: 但是

  • 我在找到一种将道具传递给组件子组件的方法时遇到了一点麻烦。我想做的是有一个组件(ParentComponent)它有一个函数(toggleSuccess())来改变它自己的状态(例如一个名为成功的布尔值),并将该函数作为onClick道具传递给它的所有子函数。理想情况下,我可以将各种组件放入ParentComponent,它们都可以通过onClick道具访问toggleSuccess()。有没有一

  • 也许有一种方法可以将组件绑定到事件? 在使用React一年多后,在Sebastien Lorber的回答的激励下,我得出结论,将子组件作为参数传递给父母中的函数实际上不是React的方式,也不是一个好主意。我换了答案。

  • 问题内容: 我正在尝试将现有的react-router 3.0.0应用程序迁移到4.0.0。在路由器内,我需要传递与其子代无关的其他非路由选择。 在早期版本的react- router中,有一些技巧性的方法可以实现此目的,本文对此进行了详细介绍。我个人最喜欢的是使用的这种方法,因为它可以确保复制所有的react-router道具: 到目前为止,我的新App组件如下所示: 在保留提供的路由器道具的同