是否可以在不渲染HTML的情况下使用React?


70

我想知道是否可以使用React进行逻辑并将数据发送回javascript函数,而无需呈现任何html。我正在考虑的组件是您将一些数据传递到的组件,它将在react之外将数据发送回javascript函数。我知道这是可以做到的,而我本人已经完成了这一部分,但是我不确定如果不呈现所需的HTML,您将如何做到这一点。这甚至是反应的实际用例吗?


3
React是一个用于构建视图的库。React的哪些功能导致您想要将其用于任务?
爱德华·M·史密斯

Answers:


70

从React> = 16.2开始,可以使用以下任何版本:

render() { 
   return false; 
}

render() { 
   return null; 
}

render() { 
   return []; 
}

render() { 
   return <React.Fragment></React.Fragment>; 
}

render() { 
   return <></>; 
}

返回undefined不起作用。


我正在考虑的组件是您将一些数据传递到的组件,它将在react之外将数据发送回javascript函数。

您为什么要为此创建一个组件?大多数时候,在现有组件中使用常规js函数就足够了。

例如,一个用例是在安装组件时设置副作用,而在卸下组件时将其拆下。例如,如果您的ReactNative移动应用程序具有纵向方向,则可以想象一个<Landscape/>组件,该组件在安装时会暂时允许以横向显示应用程序,而在卸载时,会将方向重置为应用程序默认设置。您可以肯定地在现有组件上管理此方向更改,但是创建专用组件可能更方便和可重用。

请注意,React也可以在服务器端运行,因此我想可以以不涉及任何DOM修改(但可能仅涉及虚拟DOM计算)的方式使用它。


1
不,render()方法不能返回空数组或其他React组件以外的任何东西。
andreypopp 2014年

25

1
的确,看起来在0.11是新的:facebook.github.io/react/blog/2014/07/17/...

1
为什么要返回false?这不是render()在问一个二进制问题吗?null在这种情况下,您应该返回,因为这就是您要渲染的内容。
Alex McMillan

1
在我回答这个问题时偶然发现:“为什么要为此创建一个组件?一些常规的javascript函数不够吗?” 至少在我的情况下,我要使用该体系结构附带的漂亮框架功能,例如使用react-redux将组件连接到我的状态并传递props以及具有propTypes。对于某些人来说,这可能还不够充分。我很想听听相反的意见。
buddyp450 '17

22

只是为了澄清Benno的评论。该ReactComponent.render方法文档状态:

您还可以返回nullfalse表示您不希望呈现任何内容。在幕后,React渲染了一个<noscript>标签以与我们当前的差异算法一起工作。返回null或时falsethis.getDOMNode()将返回null


14

有可能的。react-router是一个库的示例,其中的组件未呈现HTML。参见https://github.com/rackt/react-router

这是react-fouter的Route组件,其中render方法返回false:


const Route = React.createClass({

  statics: {
    createRouteFromReactElement
  },

  propTypes: {
    path: string,
    component,
    components,
    getComponent: func,
    getComponents: func
  },

  /* istanbul ignore next: sanity check */
  render() {
    invariant(
      false,
      '<Route> elements are for router configuration only and should not be rendered'
    )
  }

})

5

是的,在延迟加载组件的情况下,这很有可能并且非常有用。

考虑一下带有react-router的示例。

import React from 'react'
import { Route, Link } from 'react-router-dom'

function asyncComponent(getComponent) {
  return class AsyncComponent extends React.Component {
    static Component = null;
    state = { Component: AsyncComponent.Component };

    componentWillMount() {
      if (!this.state.Component) {
        getComponent().then(Component => {
          AsyncComponent.Component = Component
          this.setState({ Component })
        })
      }
    }
    render() {
      const { Component } = this.state
      if (Component) {
        return <Component {...this.props} />
      }
      return null
    }
  }
}

const Page1 = asyncComponent(() =>
  System.import('./Page1.js').then(module => module.default)
)
const Page2 = asyncComponent(() =>
  System.import('./Page2.js').then(module => module.default)
)
const Page3 = asyncComponent(() =>
  System.import('./Page3.js').then(module => module.default)
)

const ParentComponent = () => (
  <div>
    <ul>
      <li>
      <Link to="/page1">Page1</Link>
      </li>
      <li>
      <Link to="/page2">Page2</Link>
      </li>
      <li>
      <Link to="/page3">Page3</Link>
      </li>
    </ul>
    <div>
      <Route path="/page1" component={Page1}/>
      <Route path="/page2" component={Page2}/>
      <Route path="/page3" component={Page3}/>
    </div>
  </div>
)
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.