将 prop 从子级传递给父级 React

2023-12-10

In WeatherForecast组件我需要传递函数的返回值appColor进入财产。那么该财产来自WeatherForecast需要传入className of app成分。新的反应。不确定如何将属性从子级传递到组件。

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>  

    );
  }
}

您可以将函数从父级传递给子级,子级可以使用颜色调用该函数(几乎像事件处理程序一样操作)。当应用程序接收到颜色时,使用 .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>
    );
  }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 prop 从子级传递给父级 React 的相关文章

随机推荐

  • 在R中,如何在将对象发送到函数后获取对象的名称?

    我正在寻找相反的get 给定一个对象名称 我希望直接从该对象中提取代表该对象的字符串 简单的例子foo是我正在寻找的函数的占位符 z lt data frame x 1 10 y 1 10 test lt function a mean x
  • 收到 org.postgresql.util.PGobject 类型的对象

    我可以从代码中将几何数据插入数据库 但只能使用 SQL 编辑器 例如 PGAdmin III 查询数据 我无法将几何数据检索回我的代码 每次尝试都会以 已接收类型为 org postgresql util PGobject 的对象 结束 即
  • 如何在 iOS 中以编程方式添加 UITabBarItem 的标识符类型?

    我想在 iOS 中以编程方式添加 UITabBarItem 在界面生成器中 我们可以轻松选择搜索 收藏夹 最近选项卡等标识符 我想知道如何以编程方式添加这些标识符类型 您将其添加为 UIViewController 属性吗 你创建一个UIT
  • 如何捕获 ASP.NET 应用程序中未处理的异常?

    在一次 NET考试中我遇到了这个问题 您可以使用以下哪项来捕获应用程序中未处理的异常 服务器错误 页面错误 应用程序错误 响应错误 错误事件 我知道它的Application Error 但我想知道的是其他的是什么 通过一些谷歌搜索 我发现
  • 无法从我自己的 angularjs api 获取 JSONP 数据

    我正在尝试从我自己的 api 获取数据 但无法成功将数据获取到 vegdata 变量 这是控制器代码 scope filterText null scope vegdata scope init function url http 192
  • WPF TreeView 数据绑定以隐藏/显示展开/折叠图标

    我实现了一个 WPF 按需加载树视图 如中所述this 非常好的 文章 在上述解决方案中 使用虚拟元素来保留展开 图标 树视图项目行为 当用户单击扩展器时 虚拟项目将替换为真实数据 我想通过添加属性来完善模型public bool HasC
  • 哪些 Swing 组件方法是线程安全的?

    根据摇摆教程 一些 Swing 组件方法在 API 规范中被标记为 线程安全 这些可以从任何线程安全地调用 所有其他 Swing 组件方法都必须从事件分派线程调用 忽略此规则的程序可能在大多数情况下都能正常运行 但会出现难以重现的不可预测的
  • 无法创建 /dev/stdout:没有这样的设备或地址

    我想通过节点运行 shell 命令并捕获标准输出的结果 我的脚本在 OSX 上运行良好 但在 Ubuntu 上不行 我已将问题和脚本简化为以下节点脚本 var execSync require child process execSync
  • python中将edgelist导入igraph的格式

    igraph 接受的用于导入 python 的边缘列表格式是什么 包含加权边缘的文本文件应该是什么样子 我以前曾在 R 中使用过 igraph 但在我需要使用的机器上没有安装有效的 R 所以我陷入了 python 困境 我有一个 egeli
  • Python Matplotlib 滑块已绘制但不移动或更新

    我正在尝试绘制一个散点图 其中一个点根据滑块调整的参数移动 我需要参数是坐标列表中的位置 我拥有它 因此可以绘制散点图 并且我可以通过更改位置来手动移动点 但是当我尝试实现滑块时 它会显示 但无法更新绘图 任何帮助都会很棒 到目前为止我所拥
  • Catia VBA 到 .CATScript(针对“集合”类型)

    在我的 VBA 代码中 我使用以下内容 Dim docsToSave As Scripting Dictionary Set docsToSave New Scripting Dictionary Dim toRemove As Colle
  • Matlab 上 Z 归一化 (z-score) 函数的反函数

    在Matlab R2014a中我有信心使用z分数 x 功能 function z mu sigma zscore x flag dim if isequal x z return end if nargin lt 2 flag 0 end
  • 在 JPanel 上添加 .GIF 时显示黑色方块

    我的问题是 当将 GIF 添加到 JPanel 时 它会显示 GIF 的黑色方形背景 在JPanel上添加时的结果 当我使用这一行时会发生这种情况 p2 add loadBar where p2 new JPanel 但是 当我在 JFra
  • 在 swift 中使用简单 Ping (iOS)

    我正在尝试使用 Apple 的类 Simple Ping 但我无法使其正常工作 当我运行示例 mac os x 项目时 它正在工作 2015 06 17 00 03 22 569 SimplePing 20386 3133535 ping
  • Angular 5 插入动态输入属性

    我想将动态属性插入到输入 html 标记中 但我不知道该怎么做 我从组件端得到了这段代码 import Component OnInit from angular core Component selector app transclusi
  • Web 套接字在 Firefox 12 中不工作

    Firefox 无法与服务器 ws 192 168 0 155 5555 socket server3 php 建立连接 document ready function if WebSocket in window alert not av
  • 将 jQuery 自动完成与 Flask 结合使用

    这个问题之前已经被问过 我想我已经做了我在那里看到的事情 但我真的不知道我做错了什么 我对 jQuery 了解不多 但我会尽力解释我想要做什么 我想根据数据库中的查询自动完成 所以我的模板中有这样的内容
  • 张量流构建错误

    我在构建 Tensorflow 1 1 0 时遇到此错误 Starting local Bazel server and connecting to it ERROR home bishal cache bazel bazel bishal
  • ASP.NET Core 5 和 6 JWT 身份验证始终抛出 HTTP 401 代码

    我想在 ASP NET Core 中实现基于 JWT 的安全性 目前我想要它做的就是读取按钮中的令牌 Html ActionLink Test Oper Home 授权标头并根据我的标准验证它们 我不知道错过了什么 但它总是返回 HTTP
  • 将 prop 从子级传递给父级 React

    In WeatherForecast组件我需要传递函数的返回值appColor进入财产 那么该财产来自WeatherForecast需要传入className of app成分 新的反应 不确定如何将属性从子级传递到组件 class Wea