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)
Step 1: Confirm the architecture of your system d...
I remember when I was interviewing for my current...
Download the MySQL installation package. I downlo...
1. Implementation principle of Nginx load balanci...
The <tbody> tag is used to define the style...
The decompressed version of mysql5.7.18 starts th...
Nginx is a powerful, high-performance web and rev...
First look at the code and effect↓ <style> ...
<br />This is not only an era of information...
Preface In the development of actual projects, we...
This article originated from the homework assignm...
Check whether your cuda is installed Type in the ...
1. pc-reset PC style initialization /* normalize....
Table of contents Congruent and Incongruent congr...
Table of contents 1. Location Object 1. URL 2. Pr...