美文网首页
React插槽使用

React插槽使用

作者: 未路过 | 来源:发表于2022-11-12 23:32 被阅读0次
image.png

children实现插槽

image.png

有多个的化,children就算数组

        <Hello>
          <button>1</button>
          <button>2</button>
          <button>3</button>
        </Hello>
--------------------------
    return (
      <div className="box">
        <div className="left">{this.props.children[0]}</div>
        <div className="center">{this.props.children[1]}</div>
        <div className="right">{this.props.children[2]}</div>
      </div>
    );
image.png

只有一个的话,就直接用children

    <Hello>
          <button>1</button>
        </Hello>
-----------------------------------------
    return (
      <div className="box">
        <div className="left">{this.props.children}</div>
      </div>
    );
image.png

如果只想让别人传一个的化,就给children添加属性。

import React, { Component } from "react";
import "./Hello.css";
import PropTypes from "prop-types";
export class Hello extends Component {
  render() {
    console.log(this.props); //{children: Array(3)}
    return (
      <div className="box">
        <div className="left">{this.props.children[0]}</div>
        <div className="center">{this.props.children[1]}</div>
        <div className="right">{this.props.children[2]}</div>
      </div>
    );
  }
}
Hello.propTypes = {
  children: PropTypes.element,
};

export default Hello;

这样传多个的化,就会报错。


image.png

如果想传入多个

Hello.propTypes = {
  children: PropTypes.array,
};

这个时候传入一个button就会报错。

这种方法对index要求很高,使用的chilfren的索引错了,最后渲染的位置就错了。

props实现插槽(推荐)

image.png
export class App extends Component {
  render() {
    return (
      <div>
        <Hello
          leftSlot={<div>1</div>}
          rightSlot={<div>2</div>}
          centerSlot={<div>3</div>}
        ></Hello>
      </div>
    );
  }
import React, { Component } from "react";
import "./Hello.css";
import PropTypes from "prop-types";
export class Hello extends Component {
  render() {
    const { leftSlot, rightSlot, centerSlot } = this.props;
    return (
      <div className="box">
        <div className="left">{leftSlot}</div>
        <div className="center">{centerSlot}</div>
        <div className="right">{rightSlot}</div>
      </div>
    );
  }
}
Hello.propTypes = {
  children: PropTypes.array,
};

export default Hello;

也可以写成变量形式

import React, { Component } from "react";
import Hello from "./components/Hello/index";

export class App extends Component {
  render() {
    const leftSlot = <div>1</div>;
    const rightSlot = <div>2</div>;
    const centerSlot = <div>3</div>;
    return (
      <div>
        <Hello
          leftSlot={leftSlot}
          rightSlot={rightSlot}
          centerSlot={centerSlot}
        ></Hello>
      </div>
    );
  }
}

export default App;

作用域插槽

  <TabControl 
          titles={titles} 
          tabClick={i => this.tabClick(i)}
           itemType={item => <button>{item}</button>}
        />


子组件
  {itemType(item)}
import React, { Component } from 'react'
import TabControl from './TabControl'

export class App extends Component {
  constructor() {
    super()

    this.state = {
      titles: ["流行", "新款", "精选"],
      tabIndex: 0
    }
  }

  tabClick(tabIndex) {
    this.setState({ tabIndex })
  }

  getTabItem(item) {
    if (item === "流行") {
      return <span>{item}</span>
    } else if (item === "新款") {
      return <button>{item}</button>
    } else {
      return <i>{item}</i>
    }
  }

  render() {
    const { titles, tabIndex } = this.state

    return (
      <div className='app'>
        <TabControl 
          titles={titles} 
          tabClick={i => this.tabClick(i)}
          // itemType={item => <button>{item}</button>}
          itemType={item => this.getTabItem(item)}
        />
        <h1>{titles[tabIndex]}</h1>
      </div>
    )
  }
}

export default App
import React, { Component } from 'react'
import "./style.css"

export class TabControl extends Component {
  constructor() {
    super()

    this.state = {
      currentIndex: 0
    }
  }

  itemClick(index) {
    // 1.自己保存最新的index
    this.setState({ currentIndex: index })

    // 2.让父组件执行对应的函数
    this.props.tabClick(index)
  }

  render() {
    const { titles, itemType } = this.props
    const { currentIndex } = this.state

    return (
      <div className='tab-control'>
        {
          titles.map((item, index) => {
            return (
              <div 
                className={`item ${index === currentIndex?'active':''}`} 
                key={item}
                onClick={e => this.itemClick(index)}
              >
                {/* <span className='text'>{item}</span> */}
                {itemType(item)}
              </div>
            )
          })
        }
      </div>
    )
  }
}

export default TabControl

相关文章

  • React插槽使用

    children实现插槽 有多个的化,children就算数组 只有一个的话,就直接用children 如果只想让...

  • React之children的特殊用法

    平常我们在react使用children的用法大多都是和vue的slot插槽那样去使用,但是react中的chil...

  • vue 插槽 slot

    插槽使用 普通插槽 具名插槽 使用具名插槽 从插槽里面传值出来如何接收? 如: 如何判断某个插槽是否被使用 组件内...

  • 【Vue8】插槽

    插槽很重要!插件和模块中会经常使用插槽。 插槽的使用场景 即什么时候使用插槽?比如说这样: 我是插槽slot 父组...

  • vue 插槽slot

    插槽的定义: 插槽的使用:

  • Vue 插槽

    1.插槽的简单使用 在组件中声明一个插槽 使用组件时 可以替换插槽 如果不替换插槽就使用默认值 2.具名...

  • react 第十一章 react props的children属

    react 里面的children 相当于vue里面的插槽、、

  • # Portals

    插槽:将一个react元素渲染到指定的DOM容器中 ReactDOM.createPortal(React 元素,...

  • vue插槽

    vue插槽slot的理解与使用 vue slot插槽的使用介绍及总结

  • Flutter 局部路由实现

    Flutter是借鉴React的开发思想实现的,在子组件的插槽上,React有this.props.childre...

网友评论

      本文标题:React插槽使用

      本文链接:https://www.haomeiwen.com/subject/wkwbtdtx.html