React - Composite Component (Layout) / Container Component

About

React Layout / Composition -

There is three methods to create a composite component from html component / or composite

Parent components can interact with their children:

  • via props
  • but also Refs (They provide a way to access DOM nodes or React elements created in the render method)

Methods

Layout as a Higher Order Component

Layout as a Higher Order Component (decorator)

  • The Layout as High component Component
import Header from './Header'

const layoutStyle = {
  margin: 20,
  padding: 20,
  border: '1px solid #DDD'
}

const withLayout = Page => {
  return () => (
    <div style={layoutStyle}>
      <Header />
      <Page />
    </div>
  )
}

export default withLayout
  • All pages
import withLayout from '../components/MyHocLayout'

const Page = () => <p>Hello World</p>

export default withLayout(Page)

Layout as component with a prop

import Header from './Header'

const layoutStyle = {
  margin: 20,
  padding: 20,
  border: '1px solid #DDD'
}

const Layout = props => (
  <div style={layoutStyle}>
    <Header />
    {props.content}
  </div>
)

export default Layout
import Layout from '../components/MyContentLayout.js'

const indexPageContent = <p>Hello World</p>

export default function Index() {
  return <Layout content={indexPageContent} />
}

Layout with a props.children

example props.children

a WelcomeDialog is a special case of Dialog

  • The Base Components using the children props which makes it a container.
function Dialog(props) {
  return (
    <div className="dialog">
      <h1 className="Dialog-title">
        {props.title}
      </h1>
      <p className="Dialog-message">
        {props.message}
      </p>
      {props.children}
    </div>
  );
}
  • The CSS
.dialog {
      border: 1px solid black;
      padding: 10px;
}
  • The specialized
function WelcomeDialog() {
  return (
    <Dialog
      title="Welcome"
      message="Thank you for being you!" >
      <p>You are worth the whole world</p>
      </Dialog>
  );
}
  • The standard render and HTML placeholder
ReactDOM.render(
  <WelcomeDialog />,
  document.getElementById('root')
);
<div id="root"></div>

example props.children on 2 pages

import Header from './Header'

const layoutStyle = {
    margin: 20,
    padding: 20,
    border: '1px solid #DDD'
}

const Layout = props => (
    <div style={layoutStyle}>
        <Header />
        {props.children}
    </div>
)

/** This format works also
export default ({ children }) => (
  <div style={layoutStyle}>
    {children}
  </div>
)
*/

export default Layout
  • A page with this layout
import Layout from '../components/MyLayout.js'

export default function Index() {
  return (
    <Layout>
      <p>Hello World</p>
    </Layout>
  )
}

Documentation / Reference





Discover More
React

is a virtual dom technology that permits to create HTML page or components based on the concepts that: If the data (state) changes, the UI should change too. Learn once, write anywhere Babel for...
React - Children Element

When you create an element, you pass one or more children elements that will build the React DOM tree. componentyou create a component Related: See also The below example codes are created with JSX....
React - Component (User-defined Element)

A component is a user-defined react element in the React tree. Components let you split the UI into independent, reusable pieces, and think each piece in isolation. Conceptually, components are like...
React - Higher Order Component (HOC)

Higher Order Component is a function that takes a component and returns a new component. It's not really a component, more a design pattern known as decorator. Redux’s connect Relay’s...
React - Redux

React Redux is a binder for React of the Redux state management. Two steps: You made the redux store available globally with a You bind a presentational component with the function. reactjs/react-reduxReact...
React Framework - Next.js

Next is a React framework for server-rendered React applications. isomorphic apps (code that runs in the browser or in node) note from the getting started...
What is the React Virtual DOM? Components Tree/Hierarchy

The React tree is: a tree composed of React node that implements a virtual DOM system known as the React DOM where only the updates are applied to: the real DOM (browser DOM) or for Native...



Share this page:
Follow us:
Task Runner