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 18.04 MySQL 8.0 installation and configuration method graphic tutorial

This article shares the installation and configur...

Native JS to implement drag position preview

This article shares with you a small Demo that ad...

Native JS to implement sharing sidebar

This article shares a sharing sidebar implemented...

How to install multiple mysql5.7.19 (tar.gz) files under Linux

For the beginner's first installation of MySQ...

Specific use of Linux gcc command

01. Command Overview The gcc command uses the C/C...

js data types and their judgment method examples

js data types Basic data types: number, string, b...

mysql join query (left join, right join, inner join)

1. Common connections for mysql INNER JOIN (inner...

How to deploy Angular project using Docker

There are two ways to deploy Angular projects wit...

MySQL query learning basic query operations

Preface MySQL is the most popular relational data...

Explanation of Dockerfile instructions and basic structure

Using Dockerfile allows users to create custom im...

jQuery implements all selection and reverse selection operation case

This article shares the specific code of jQuery t...