Giter VIP home page Giter VIP logo

react-aux's Introduction

react-aux

NPM version Twitter Follow

A self-eradicating component for rendering multiple elements.

If you are using React 16.2 or higher

React 16.2 includes support for a new React.Fragment API which works very similar to this library. If you don't know which one to pick, we recommend the version provided by React. This library mostly exists for those who can't upgrade to React 16.2 yet.

Motivation

Prior to React v16, returning multiple elements from a component required to wrap them in an auxiliary element, e.g.

const Root = () => {
  return <div>
    <p>Hello, World!</p>
    <p>I am a demo for react-aux.</p>
  </div>;
};

The latter produces the following DOM:

<div>
  <p>Hello, World!</p>
  <p>I am a demo for react-aux.</p>
</div>

Starting with React v16, a single component can return multiple components without a wrapping element, e.g.

const Aux = (props) => {
  return props.children;
};

const Root = () => {
  return <Aux>
    <p>Hello, World!</p>
    <p>I am a demo for react-aux.</p>
  </Aux>;
};

The latter produces paragraph elements without the wrapping node:

<p>Hello, World!</p>
<p>I am a demo for react-aux.</p>

As you can see, react-aux is literally just 3 lines of code. Therefore, you could implement it in your own codebase without using react-aux. However, props => props.children on its own does not explain the intent. react-aux as an abstraction serves the purpose of enabling a self-documenting code, i.e. the next time you see someone doing:

import Aux from 'react-aux';

const Root = () => {
  return <Aux>
    <p>Hello, World!</p>
    <p>I am a demo for react-aux.</p>
  </Aux>;
};

You will know exactly what is the intent of the code.

Related articles

FAQ

Whats the difference from using an array?

You can use an array if you assign each React$Element a pseudo-property key with a unique value, e.g.

import Aux from 'react-aux';

const Root = () => {
  return [
    <p key='p1'>Hello, World!</p>,
    <p key='p2'>I am a demo for react-aux.</p>
  ];
};

However, it requires manually ensuring key uniqueness and I am too lazy for this.

Whats with the name?

"aux" is a convention I've been using ever since I remember starting to write HTML/ CSS. Auxiliary element is something that does not have semantic purpose but exist for the purpose of grouping elements, styling, etc.

react-aux's People

Contributors

cdeutsch avatar gaearon avatar gajus avatar jimthedev avatar nickbreaton avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

react-aux's Issues

Git doesn't like indexing aux

Not sure if I am simply doing something wrong, I have looked up this issue on other sites and didn't really find much. When I include this library in a project then upload that project to Github I get the error "Fatal Error: Could not find file '[path]/Aux.js.'"

The only way for me to upload my project is to add aux.js to my gitignore and then simply having people install it when they pull the repo. I also tried just writing an aux.js file without installing it from npm and got the same error.

Support react 15

What if we actually support react 15 in this component, so that people who are still not migrated to React 16 can start using it?

children element [] issues

Unfortunately when wrapping elements most of the time we're seeing this error:
{ children: Element[]; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes & Readonly<{ children?: ReactNode; }> & Reado...'.

Aux is a reserved filename in Windows

I wanted to delete the node_modules folder and recreate it on one of my projects and I found out that Aux is a reserved filename on Windows, thus I cannot easily delete the react-aux folder. The only viable solution to delete it was in this Stackoverflow answer

rd "\\?\C:\holds bad subdir" /s

Maybe it would be possible to rename Aux.js to something else? For reference, here an explanation about the reserved filenames:

Naming Files, Paths, and Namespaces

CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9. Also avoid these names followed immediately by an extension; for example, NUL.txt is not recommended. For more information, see Namespaces.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.