Core concept
In Vue, slots allow parent components to pass HTML structures to children, enabling more flexible component reuse. Named slots allow the parent component to pass multiple different HTML structures to the child component, while scoped slots allow the child component to pass data to the parent component so that the parent component can render different content based on this data.
In React, we don't have a direct concept of slots, but it can be done throughand functions as props to implement similar functions.
Implement named slots
In React, we can simulate named slots by passing different props. Each prop represents a slot, and its value can be a React element or function.
Sample code
// components/ import React from 'react'; function Layout({ header, content, footer }) { return ( <div className="layout"> <header className="layout-header">{header}</header> <main className="layout-content">{content}</main> <footer className="layout-footer">{footer}</footer> </div> ); } export default Layout;
// import React from 'react'; import Layout from './components/Layout'; function App() { return ( <Layout header={<h1>Website title</h1>} content={<p>This is the main content of the website。</p>} footer={<small>all rights reserved © 2024</small>} /> ); } export default App;
Code explanation
-
:
- Define a
Layout
Component, it receives three props:header
、content
andfooter
。 - The values of these props can be any React element, for example
<h1>
、<p>
、<span>
wait. -
Layout
The component renders these props into the corresponding HTML structure.
- Define a
-
:
- Introduced
Layout
Components. - exist
App
In the component, weLayout
The component passes three props:-
header
:one<h1>
element, as the title of the page. -
content
:one<p>
Element, as the main content of the page. -
footer
:one<small>
element, as the footer of the page.
-
- In this way, we implement functions similar to Vue named slots.
- Introduced
Implement scope slots
In React, we can implement scoped slots by passing functions as props. The child component calls this function and passes the data to it as a parameter, and the parent component renders different content based on this data.
Sample code
// components/ import React from 'react'; function List({ items, renderItem }) { return ( <ul> {((item, index) => ( <li key={index}>{renderItem(item, index)}</li> ))} </ul> ); } export default List;
// import React from 'react'; import List from './components/List'; function App() { const items = [ { id: 1, name: 'apple', price: 5 }, { id: 2, name: 'banana', price: 3 }, { id: 3, name: 'orange', price: 4 }, ]; const renderItem = (item, index) => { return ( <div> {index + 1}. {} - price: {}Yuan { > 4 && <span style={{ color: 'red' }}> (expensive)</span>} </div> ); }; return ( <div> <h1>Product List</h1> <List items={items} renderItem={renderItem} /> </div> ); } export default App;
Code explanation
-
:
- Define a
List
Component, which receives two props:items
andrenderItem
。 -
items
is an array containing data to be rendered. -
renderItem
is a function that renders each list item. -
List
Component traversalitems
array and call each elementrenderItem
function, passing elements and indexes as parameters to it. -
renderItem
The return value of the function will be rendered to<li>
in the element.
- Define a
-
:
- Introduced
List
Components. - Define a
items
Array, containing some product data. - Define a
renderItem
function, it receives aitem
andindex
As an argument, and return a React element. - exist
renderItem
In the function, weitem
The data renders different content, such as product name, price, and whether it is too high. - exist
App
In the component, weList
The component is passeditems
Array andrenderItem
function. - In this way, we implement functions similar to Vue scoped slots.
- Introduced
More complex examples
For a better understanding, let's look at a more complex example, which contains multiple named slots and scoped slots.
// components/ import React from 'react'; function Card({ header, content, actions, renderFooter }) { return ( <div className="card"> <div className="card-header">{header}</div> <div className="card-content">{content}</div> <div className="card-actions">{actions}</div> <div className="card-footer">{renderFooter && renderFooter()}</div> </div> ); } export default Card;
// import React from 'react'; import Card from './components/Card'; function App() { const user = { name: 'Zhang San', age: 30, email: 'zhangsan@', }; const renderFooter = () => { return ( <p> User Information:{},{}age,Mail:{} </p> ); }; return ( <Card header={<h2>User Information</h2>} content={<p>This is the user's details。</p>} actions={ <button onClick={() => alert('edit')}>edit</button> } renderFooter={renderFooter} /> ); } export default App;
Code explanation
-
:
- Define a
Card
Component, which receives four props:header
、content
、actions
andrenderFooter
。 -
header
、content
andactions
are named slots, their values can be any React element. -
renderFooter
is a function that renders the footer of a card, which is a scoped slot. -
Card
The component renders these props into the corresponding HTML structure.
- Define a
-
:
- Introduced
Card
Components. - Define a
user
Object, containing some user information. - Define a
renderFooter
Function, which returns a React element containing user information. - exist
App
In the component, weCard
The component passes four props:-
header
:one<h2>
element, as the title of the card. -
content
:one<p>
Elements, as the main content of the card. -
actions
:one<button>
Element, as the action button for the card. -
renderFooter
: A function that renders the footer of a card.
-
- In this way, we implement the functions of multiple named slots and scoped slots.
- Introduced
Summarize
Through the above example, we can see that in React, we can passand functions as props to implement Vue-like named and scoped slots.
- Named slots: Simulated by passing different props, each prop represents a slot, whose value can be a React element or function.
- Scope slots: Simulate by passing functions as props, the child component calls this function and passes the data as parameters to it, and the parent component renders different content based on these data.
Although this approach is not as concise as Vue's slot syntax, it is flexible enough to meet the needs of most scenarios.
The above is the detailed content of the sample code for implementing Vue's slot function in React. For more information about React's slot function in implementing Vue, please pay attention to my other related articles!