Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React.js: Is it possible to namespace child components while still using JSX to refer to them?

So let's say I have a component called ImageGrid and it is defined as below:

window.ImageGrid = React.createClass({
    render: function() {
        return (
            <div className="image-grid">
                <ImageGridItem />
            </div>
        );
    }
});

As you can see it includes a child react component called ImageGridItem. Which is defined below.

window.ImageGridItem = React.createClass({
    render: function() {
        return (
            <div className="item-container">something</div>
        );
    }
});

This works fine as long as both are directly properties of window. But this is kind of horrible so I'd like to group up all my react components under a namespace of window.myComponents for example.

So the definitions change to:

window.myComponents.ImageGrid     = React.createClass({...});
window.myComponents.ImageGridItem = React.createClass({...});

The problem now is that as ImageGrid refers to <ImageGridItem /> in it's render() function, the JS version of this gets compiled out to JS as ImageGridItem() which of course is undefined since it's now actually myComponents.ImageGridItem() and react complains it can't find it.

Yes I realise I can just not write JSX for that component include and manually do myComponents.ImageGridItem({attr: 'val'}) but I'd really prefer to use the JSX html syntax shortcut as it's much easier to read and develop with.

Are there any ways to get this to work while still using the <ImageGridItem /> syntax for children? And if not is it possible to define the JS namespace within the JSX?

like image 960
Mike Driver Avatar asked May 14 '14 11:05

Mike Driver


People also ask

Is it possible to use React without JSX?

Using React without JSX is especially convenient when you don’t want to set up compilation in your build environment. Each JSX element is just syntactic sugar for calling React.createElement (component, props, ...children). So, anything you can do with JSX can also be done with just plain JavaScript. For example, this code written with JSX:

Can react JSX tags contain children?

JSX tags may contain children: By default, React DOM escapes any values embedded in JSX before rendering them. Thus it ensures that you can never inject anything that’s not explicitly written in your application. Everything is converted to a string before being rendered.

How to declare children of a post component in react?

// Post.js export const Post = () => { ... } As children are special properties, you don’t have to declare them when using the component, but you have to tell the component itself that children are welcome. The word children is a special word in the React world with a set meaning like function or const.

How to pass in children components and expressions with JSX?

We converted true to 'true' with String so that it’ll display on the screen. We can pass in children components, strings or expressions with JSX by putting them in between the opening and closing tags of a component and then access them in the component via props.children . We can pass in any expression, function, or components in between the tags.


1 Answers

This pull request was just merged:

https://github.com/facebook/react/pull/760

It allows you to write things like <myComponents.ImageGridItem /> in React 0.11 and newer.

That said, a proper module system is the recommended way to manage dependencies to avoid pulling in code that you don't need.

like image 116
Sophie Alpert Avatar answered Sep 28 '22 08:09

Sophie Alpert