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 ReactSince 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:
|
<<: MySQL 8.0 upgrade experience
>>: 37 Tips for a Good User Interface Design (with Pictures)
This article shares the installation and configur...
This article shares with you a small Demo that ad...
This article shares a sharing sidebar implemented...
Today, when I logged into the company's inter...
Today, due to project requirements, js is needed t...
For the beginner's first installation of MySQ...
1. Key points for early planning of VMware vSpher...
01. Command Overview The gcc command uses the C/C...
js data types Basic data types: number, string, b...
1. Common connections for mysql INNER JOIN (inner...
Table of contents 1. Introduction 2. Installation...
There are two ways to deploy Angular projects wit...
Preface MySQL is the most popular relational data...
Using Dockerfile allows users to create custom im...
This article shares the specific code of jQuery t...