简体   繁体   English

没有JSX的ReactJS

[英]ReactJS without JSX

I am using React to generate a decently huge and complicated DOM tree structure but I choose not to go with JSX ( just to avoid the eventual and unavoidable transformation from JSX to JS again ). 我正在使用React生成一个体积庞大且复杂的DOM树结构,但我选择不使用JSX( 只是为了避免从JSX到JS的最终和不可避免的转换 )。 Some portions of this DOM will be generated or visible to user based on certain (if-else) conditions. 根据某些(if-else)条件,用户可以生成或看到此DOM的某些部分。 In another case, a loop might be required to generate a few HTML elements and so on. 在另一种情况下,可能需要循环来生成一些HTML元素,依此类推。

However, I could not find any good atricles around that explain React without JSX. 但是,在没有JSX的情况下解释React,我找不到任何好的atricles。

Hence, please guide and show how to use ReactJS without JSX and using Factory, class, components and others. 因此, 请指导并展示如何在没有JSX的情况下使用ReactJS并使用Factory,类,组件等。

There are not enough documentations on this. 关于此,没有足够的文件。

It seems like you're aware JSX converts to JS. 看起来你知道JSX转换为JS。

So instead of writing JSX ... 所以不要写JSX ......

// jsx
var data = [1,2,3];

var nodes = <ul>{data.map(function(p,i) {
  return <li><Person key={i} id={p} /></li>; 
})}</ul>;

Just write the JS instead ! 只需写JS吧!

// js
var data = [1, 2, 3];

var nodes = React.createElement(
  "ul",
  null,
  data.map(function (p, i) {
    return React.createElement(
      "li",
      null,
      React.createElement(Person, { key: i, id: p })
    );
  })
);

I use the following trick (mimicking the first example on the React homepage): 我使用以下技巧(模仿React主页上的第一个示例):

const __ = Object.assign(React.createElement, React.DOM);

var HelloMessage = React.createClass({
  render: function() {
    return __.div({}, 'Hello ', this.props.name);
  }
});

ReactDOM.render(__(HelloMessage, {name:"John"}), document.getElementById('root'));

Here are all four homepage examples: 以下是所有四个主页示例:

While this code does use an unfamiliar idiom, the __. 虽然这段代码确实使用了一个不熟悉的习语,但__. prefix offers a strong visual mnemonic that is just as readable as JSX. 前缀提供了一个强大的视觉助记符,就像JSX一样可读。 I'd say more readable, thanks to the absence of close tags. 由于没有密切标签,我会说更具可读性。

NoJSX is a lightweight JSON based alternative. NoJSX是一种基于JSON的轻量级替代方案。 You can create a tree like the below... 您可以创建如下所示的树...

- div.container.container--app
    -- div.jumbotron
        --- h1
        --- p

... by defining the tree structure of elements represented with properties including children , props and type . ...通过定义用属性(包括childrenpropstype表示的元素的树结构。 These mirror the arguments for React.createElement . 这些镜像反映了React.createElement的参数。

const pageHeader = {
  children: [
    {
      children: 'Hello World.',
      type: 'h1'
    },
    {
      children: 'Foobar.',
      type: 'p'
    }
  ],
  props: { className: 'jumbotron' },
  type: 'div'
};

const templateData = {
  children: [
    {
      props: { title },
      type: Helmet
    },
    pageHeader
  ],
  props: { className: 'container container--app' },
  type: 'div'
};

const template = new NoJSX(templateData);
return template.compile();

This is my preferred approach: https://github.com/simonrelet/react-pure-html-component-loader 这是我的首选方法: https//github.com/simonrelet/react-pure-html-component-loader

This component is supposed to let you write pure html templates to use as components in react without mixing HTML with javascript in such a horrible way. 这个组件应该让你编写纯html模板用作反应中的组件,而不会以如此可怕的方式混合HTML与javascript。

I see the repository has not been updated for a year, so this guy needs help if we want to be able to use react without that horrible JSX mixed inside the scripts. 我看到存储库已经有一年没有更新了,所以如果我们想要能够使用反应而没有那些可怕的JSX混合在脚本中,那么这个人需要帮助。

📖 Official documentations 📖 官方文件

Here is an official documentation how to use React without JSX. 这是一个如何在没有JSX的情况下使用React的官方文档。

Beside the mentioned alternative libraries to JSX, 除了提到的JSX替代库之外,

I can suggest React on lambda which makes coding on react in more functional way. 我可以建议在lambda上使用React,它使编码以更多功能的方式进行反应。

HyperScript Helpers are a JavaScript based option. HyperScript Helpers是基于JavaScript的选项。

Less verbose than createElement and often JSX, but still plain JavaScript, so for example JavaScript comments, arrays, objects and functions are used, 比createElement和JSX更简洁,但仍然是纯JavaScript,所以例如使用JavaScript注释,数组,对象和函数,

JSX: JSX:

<MyComponent className='className'>Hi</MyComponent>

versus HyperScript Helpers: 与HyperScript助手:

MyComponent('.className', ['Hi'])

Example HTML to HyperScript converter . 示例HTML到HyperScript转换器

Usage from React HyperScript Helpers library : React HyperScript Helpers库的 用法

DOM components are really easy to use. DOM组件非常易于使用。 Just import and go. 只需导入即可。

import { div, h2 } from 'react-hyperscript-helpers';

export default () => div('.foo', [ h2('Hello, world') ]);

For custom components you can either create a factory function or use the h function, similar to react-hyperscript. 对于自定义组件,您可以创建工厂函数或使用h函数,类似于react-hyperscript。

//MyComponent
import { div, hh } from 'react-hyperscript-helpers';

export default hh(() => div('Nifty Component'));

//Container
import MyComponent        from './MyComponent';
import SomeOtherComponent from 'who-whats-its';
import { div, h }         from  'react-hyperscript-helpers';

export default () => div('.foo', [
  MyComponent(),
  h(SomeOtherComponent, { foo: 'bar' })
]);

You could also try the library we made at Uqbar: 您也可以尝试我们在Uqbar制作的图书馆:

https://www.npmjs.com/package/njsx https://www.npmjs.com/package/njsx

It's quite easy to use and provides a cleaner interface than the React out-of-the-box interface. 它非常易于使用,并提供比React开箱即用界面更清晰的界面。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM