[react]: Intuitively Transclude another Component - reactjs

Say I have a component Parent
<Parent>
<Component1 prop1={prop1} prop2={prop2}/>
<Component2 prop3={prop3} />
</Parent>
Normally, passing prop1,2, and 3 would render a perfectly usable Parent component. However, I want this component to be just as intuitive and dynamic like the components in ui libraries (for example, antd does this).
I would like to give the developer the option of transcluding his own component1. His usage would look a little like this:
<Parent prop3={prop3}>
<Component1>
<div> {prop1} </div>
<div> {prop2} </div>
</Component1>
</Parent>
So my questions are:
How would I allow possible? transclusion of a specific component on my component?
is this okay(not necessarily good, but practical) practice?
Thanks!

I think it is perfectly fine to do so if that's what you want for your API. It is also pretty simple.
To achieve this you need to use props.children. It will then include whatever is in between the opening and closing tag of the component:
In your example, then render function of Component would be:
render() {
return <div className="outer-shell">
{this.props.children}
</div>
}

You can use the parent component as a container which can hold other components.
class Parent extends Component{
render(){
return <div>{this.props.children}</div>
}
}
And this can be used like,
class SomeComponent{
render(){
return <Parent><Child1/><Child2></Parent>
}
}

Related

React - is there a way to return JSX which contains braces? [duplicate]

I'm new to React and I'm trying to figure out the purpose/use of <MyComponent></MyComponent> vs <MyComponent />. I can't seem to find information on anything except self-closing tags.
I've created a basic tab scroller as a JSFiddle using the self-closing <MyComponent /> and subsequent props, and I'm wondering if there's a better way to write in React than what I've done.
class TabScroller extends React.Component {
render() {
return (
<div className="tabScroller">
<div className="NavList">
<TabNav handleClick={this.handleNavClick} />
<TabList
tabs={this.state.tabs}
activeTab={this.state.activeTab}
scrollPosition={this.state.scrollPosition}
handleClick={this.handleTabClick}
/>
</div>
<TabContent content={this.state.tabs[this.state.activeTab].content} />
</div>
);
}
}
// ========================================
ReactDOM.render(
<TabScroller />,
document.getElementById('root')
);
In React's JSX, you only need to write <MyComponent></MyComponent> when the component has child components, like this:
<MyComponent>
<Child />
<Child />
<Child />
</MyComponent>
If there is nothing between <MyComponent> and </MyComponent>, then you can write it either <MyComponent/> or <MyComponent></MyComponent> (but <MyComponent/> is generally preferred). Details in Introducing JSX.
Just as a side note, you'd access those children in your component via the special props.children property. More in JSX in Depth: Children in JSX.
Note that this is very much not like HTML or XHTML. It's its own (similar) thing with different rules. For instance, in HTML, <div/> is exactly the same thing as <div>: A start tag, for which you must eventually have an end tag. Not so JSX (or XHTML). The rules for HTML are that void elements (elements that never have markup content, such as br or img) can be written with or without / before > and they never get an ending tag, but non-void elements (like div) must always have an ending tag (</div>), they cannot be self-closing. In JSX (and XHTML), they can be.
The purpose of self-closing tags is simply the fact that it is more compact. This is especially useful when said component doesn't have any children that you typically wrap around a parent.
So usually for leaf components (i.e compoents that do not have any children), you use the self-closing syntax. Like: <Component />. And even if it has props, you can do: <Component foo="bar" />.
However, remember that children is a prop, so you could technically do:
<Component children={<span>foo</span>} />
but I find it less readable and advise against it (read disclaimer below).
To summarize, these are equivalent:
<Component /> = <Component></Component>
<Component foo="bar" /> = <Component foo="bar"></Component>
<Component children={<span>foo</span>}></Component> =
<Component><span>foo</span></Component>
You can use whichever approach you prefer. Though praxis is to use the short-hand version when there are no children.
Disclaimer: While defining childen prop by its object key value will technically work, doing so is strongly discouraged as it disrupts the API as it is meant to be used. Use this version only if confident in what you are doing.

How to re-use HTML component wrapper in a sequence of child components?

I have what I thought would be a very simple question about reusing html wrappers for components. But I'm not sure what the 'proper' way to do it is.
I want to render a series of components reusing the same (complex) html wrapper for each of them. Then rendering the a set of different child components inside that wrapper.
(render 10 of these:)
<div i start>
<div start reusable wrapper>
<child component - one of ten different components, one after the other>
<div end reusable wrapper
<div i end>
I just need a high level suggestion. I feel like everything I think of is 'hacking it'.
const Layout = props => (
<div whateverattributes>
<div whateverotherattributes>
{props.children}
</div>
</div>
);
Then
<Layout>
<ChildComponent />
</Layout>

Constrain allowed children in React TypeScript

Using TypeScript in a React project is there any way to enforce some constraints on the allowed children of a component? Compile-time is preferred, but run-time could still be helpful.
In my case I have a component, call it <ClickTracker>, and it expects a single child with a callback prop onClick and it adds some extra functionality to the callback (tracking the click in an external library).
This works great as long as the child inside a <ClickTracker> does actually make use of an onClick prop (all HTML elements implement this, for example), but fails silently otherwise.
For example, this works:
<ClickTracker>
<div>Hello</div>
</ClickTracker>
But this doesn't work:
class Hello extends Component<{}, {}> {
render() {
return <div>Hello</div>
}
}
<ClickTracker>
<Hello />
</ClickTracker>
But this does work because it passes onClick to an HTML element:
class Hello extends Component<{onClick: MouseEventHandler}, {}> {
render() {
return <div onClick={this.props.onClick}>Hello</div>
}
}
<ClickTracker>
<Hello />
</ClickTracker>
As you can see I would like to have some safety around what can go inside <ClickTracker> based on the child props. Or if there's another way this could be done.
There is currently, no possible way of enforcing the children type with TypeScript.
There is more information in this issue.
Or if there's another way this could be done
Click events bubble up unless some component stopped propogation (not the default behavior for native components). So you can:
<div onClick={()=>alert('still noted')}>
<Hello />
</div>
And that div is your ClickTracker 🌹

Update React component with placeholder replaced by another component

I have a react component rendered via:
render() {
var props = this.props,
config = props.config;
return(
<section className="prompt" data-component="prompt" style={{color: config.get('promptTextColor')}}>
<div className="prompt-text-1 emphasis-font-web-safe">
<DangerouslySetInnerHtml text={config.get('text1')} />
</div>
</section>
);
}
"text1" will be something like
<div><ctaPlaceholder></ctaPlaceholder></div>
and I want to replace that ctaPlaceholder with a different component (which is imported within the component I am rendering).
I have been able to replace just using normal javascript, basically just writing a replace function within componentDidMount, but would like to replace with the full component instead of just a string.
Thanks,
Update:
Eventually I have come up with putting this inside componentDidMount:
ReactDOM.render(<Provider store={store}>
<CTA className="myClass" url={this.props.config.get('ctaUrl')} style={{color: this.props.config.get('submitColor')}}>Foo</CTA>
</Provider>, document.getElementsByTagName('cta')[0]);
I need to import store and provider in the file for this, which I don't think is specially clean as I had it before just in one place in my solution. But unless someone can help me with a better solution is they only way I was able to make it work
The best way to do this would be to render the right component based on a prop or state, that's the react way.
I can't tell your specific use case, but the core for your use case will be something like this:
renderPlaceholder() {
if (this.state.isCtaPlaceholder) {
return <DangerouslySetInnerHtml text={config.get('text1')} />;
} else {
return <OtherComponent />
}
}
render() {
var props = this.props,
config = props.config;
return(
<section className="prompt" data-component="prompt" style={{color: config.get('promptTextColor')}}>
<div className="prompt-text-1 emphasis-font-web-safe">
{ this.renderPlaceholder() }
</div>
</section>
);
You just need to decide what to render based on state/props, when and the best to do i is always dependent on the specific use case.

When should I be using React.cloneElement vs this.props.children?

I am still a noob at React and in many examples on the internet, I see this variation in rendering child elements which I find confusing. Normally I see this:
class Users extends React.Component {
render() {
return (
<div>
<h2>Users</h2>
{this.props.children}
</div>
)
}
}
But then I see an example like this:
<ReactCSSTransitionGroup
component="div"
transitionName="example"
transitionEnterTimeout={500}
transitionLeaveTimeout={500}
>
{React.cloneElement(this.props.children, {
key: this.props.location.pathname
})}
</ReactCSSTransitionGroup>
Now I understand the api but the docs don't exactly make clear when I should be using it.
So what does one do which the other can't? Could someone explain this to me with better examples?
props.children isn't the actual children; It is the descriptor of the children. So you don't have actually anything to change; you can't change any props, or edit any functionality; you can only read from it. If you need to make any modifications you have to create new elements using React.CloneElement.
https://egghead.io/lessons/react-use-react-cloneelement-to-extend-functionality-of-children-components
An example:
main render function of a component such as App.js:
render() {
return(
<Paragraph>
<Sentence>First</Sentence>
<Sentence>Second</Sentence>
<Sentence>Third</Sentence>
</Paragraph>
)
}
now let's say you need to add an onClick to each child of Paragraph; so in your Paragraph.js you can do:
render() {
return (
<div>
{React.Children.map(this.props.children, child => {
return React.cloneElement(child, {
onClick: this.props.onClick })
})}
</div>
)
}
then simply you can do this:
render() {
return(
<Paragraph onClick={this.onClick}>
<Sentence>First</Sentence>
<Sentence>Second</Sentence>
<Sentence>Third</Sentence>
</Paragraph>
)
}
Note: the React.Children.map function will only see the top level elements, it does not see any of the things that those elements render; meaning that you are providing the direct props to children (here the <Sentence /> elements). If you need the props to be passed down further, let's say you will have a <div></div> inside one of the <Sentence /> elements that wants to use the onClick prop then in that case you can use the Context API to do it. Make the Paragraph the provider and the Sentence elements as consumer.
Edit:
Look at Vennesa's answer instead, which is a better explanation.
Original:
First of all, the React.cloneElement example only works if your child is a single React element.
For almost everything {this.props.children} is the one you want.
Cloning is useful in some more advanced scenarios, where a parent sends in an element and the child component needs to change some props on that element or add things like ref for accessing the actual DOM element.
In the example above, the parent which gives the child does not know about the key requirement for the component, therefore it creates a copy of the element it is given and adds a key based on some unique identifier in the object. For more info on what key does: https://facebook.github.io/react/docs/multiple-components.html
In fact, React.cloneElement is not strictly associated with this.props.children.
It's useful whenever you need to clone react elements(PropTypes.element) to add/override props, without wanting the parent to have knowledge about those component internals(e.g, attaching event handlers or assigning key/ref attributes).
Also react elements are immutable.
React.cloneElement( element, [props], [...children] ) is almost equivalent to:
<element.type {...element.props} {...props}>{children}</element.type>
However, the children prop in React is especially used for containment (aka composition), pairing with React.Children API and React.cloneElement, component that uses props.children can handle more logic(e.g., state transitions, events, DOM measurements etc) internally while yielding the rendering part to wherever it's used, React Router <switch/> or compound component <select/> are some great examples.
One last thing that worth mentioning is that react elements are not restricted to props.children.
function SplitPane(props) {
return (
<div className="SplitPane">
<div className="SplitPane-left">
{props.left}
</div>
<div className="SplitPane-right">
{props.right}
</div>
</div>
);
}
function App() {
return (
<SplitPane
left={
<Contacts />
}
right={
<Chat />
} />
);
}
They can be whatever props that makes sense, the key was to define a good contract for the component, so that the consumers of it can be decoupled from the underlying implementation details, regardless whether it's using React.Children, React.cloneElement, or even React.createContext.

Resources