How to use React slots

How to use React slots

need

We have written a component ourselves. When referencing the component, we want to write content in the component, and the written content can be recognized and controlled by the component. Students who have used Vue will definitely think of slots immediately. React also supports slots. Next, we use react to develop a component that supports slots.

Core Idea

The three divs passed by the parent component in the child component will be passed to the child component through props by default, and then we can control the rendering of children in the child component.

Core code

// 015 import React from 'react' using slots;
import ReactDOM from 'react-dom';
// Parent component class ParentDom extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            heardData: "header data",
            footData: "Bottom data",
            contentData: "Content data"
        }
    }
    componentDidMount() {
    }

    render() {
        return (
            <div>
                <h1>015Using slots</h1>
                <ChildDom>
                    <div data-position="heard" >This is the header</div>
                    <div data-position="content" >This is the content丨{this.state.contentData}</div>
                    <div data-position="foot" >This is the bottom</div>
                </ChildDom>
            </div>
        )
    }
}
// Child component class ChildDom extends React.Component {        
    constructor(props) {
        super(props)  
    }
    render() {
        let heard,content,foot;
        this.props.children.forEach(item =>{
            if(item.props['data-position'] == 'heard'){
                heard = item
            }else if(item.props['data-position'] == 'content'){
                content = item
            }else if(item.props['data-position'] == 'foot'){
                foot = item
            }
        })  
        return (
            <div >
                <p>I am a subcomponent</p>
               <div className="heard">
                   {heard}
               </div>
               <div className="content">
                   {content}
               </div>
               <div className="foot">
                   {foot}
               </div>
            </div>
        )
    }
}

export default function () {
    ReactDOM.render(
        <ParentDom />,
        document.querySelector("#example15")
    );
}

Display effect

Two ways to implement slots in React

Since the content written in the React component will be mounted in props, this can be used to implement a slot function similar to that in Vue.

This is the outermost code

import React, { Component } from 'react'
import NavBar from './NavBar'
import NavBar2 from './NavBar2'

export default class App extends Component {
  render() {
    return (
      <div>
        <NavBar>
          <span>aaa</span>
          <strong>bbb</strong>
          <a href="/#" rel="external nofollow" rel="external nofollow" >ccc</a>
        </NavBar>

        <NavBar2 leftslot={<span>aaa</span>}
        centerslot={<strong>bbb</strong>}
        rightslot={<a href="/#" rel="external nofollow" rel="external nofollow" >ccc</a>}/>
      </div>
    )
  }
}

1. Use this.props.children[index]

import React, { Component } from 'react'

import './style.css'
export default class NavBar extends Component {
  render () {
    return (
      <div className="nav-bar">
        <div className="nav-left">
          {this.props.children[0]}
        </div>
        <div className="nav-center">
          {this.props.children[1]}
        </div>
        <div className="nav-right">
          {this.props.children[2]}
        </div>
      </div>
    )
  }
}

2. Use direct naming

import React, { Component } from 'react'

import './style.css'
export default class NavBar extends Component {
  render () {
    const {leftslot, centerslot,rightslot} = this.props
    return (
      <div className="nav-bar">
        <div className="nav-left">
          {leftslot}
        </div>
        <div className="nav-center">
          {centerslot}
        </div>
        <div className="nav-right">
          {rightslot}
        </div>
      </div>
    )
  }
}

This is the end of this article about how to use React slots. For more relevant React slot content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Use react context to implement vue slot function
  • React implements project practice similar to slots in Vue

<<:  MySQL 8.0 upgrade experience

>>:  37 Tips for a Good User Interface Design (with Pictures)

Recommend

Ubuntu 16.04 64-bit compatible with 32-bit programs in three steps

Step 1: Confirm the architecture of your system d...

HTML table tag tutorial (45): table body tag

The <tbody> tag is used to define the style...

mysql5.7.18 decompressed version to start mysql service

The decompressed version of mysql5.7.18 starts th...

Nginx request limit configuration method

Nginx is a powerful, high-performance web and rev...

Several popular website navigation directions in the future

<br />This is not only an era of information...

In-depth explanation of modes and environment variables in Vue CLI

Preface In the development of actual projects, we...

Detailed Analysis of or, in, union and Index Optimization in MySQL

This article originated from the homework assignm...

Win10 uses Tsinghua source to quickly install pytorch-GPU version (recommended)

Check whether your cuda is installed Type in the ...

Summary of commonly used CSS encapsulation methods

1. pc-reset PC style initialization /* normalize....

About the problems of congruence and inequality, equality and inequality in JS

Table of contents Congruent and Incongruent congr...

JavaScript BOM location object + navigator object + history object

Table of contents 1. Location Object 1. URL 2. Pr...