【问题标题】:Modify React Element From React Class从 React 类修改 React 元素
【发布时间】:2017-07-07 20:05:56
【问题描述】:

实时预览https://www.webpackbin.com/bins/-KoSqF5OB0IRtbBEi822

我正在尝试克隆和修改一个 React 元素对象。

我正在通过克隆并将 css 规则添加到样式表来删除 react 元素的内联样式

我正在使用这个function

我想知道如何从元素的类中访问 React Render 函数返回值?

const Div = () => (<div></div>); // not working
const Div = (<div></div>); // working

const NewDiv = noInline(Div); // calling function

【问题讨论】:

    标签: javascript reactjs higher-order-components


    【解决方案1】:

    我相信要做到这一点,您需要在 render 函数中获取对元素的引用,并且该元素应该是组件子元素之一 - 所以遍历 this.props.children 并找到您要查找的元素。像这样的:

    render () {
      let children = React.Children.toArray(this.props.children);
      children.forEach((child) => doSomething(child))
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-19
      • 2018-06-22
      • 2021-06-25
      • 2020-12-10
      • 2020-10-16
      • 2021-07-21
      • 1970-01-01
      • 2019-09-01
      相关资源
      最近更新 更多