xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

react slot component with args All In One

react slot component with args All In One

how to pass args to react props child component

https://codesandbox.io/s/react-slot-component-with-args-n11d1

OK

https://codesandbox.io/s/react-slot-component-with-args-idhib

function component, pass slot child component as Function props

import React from "react";
import ReactDOM from "react-dom";

import "./styles.css";

import FunnelChart from "./FunnelChart";
import Slot from "./Slot";

function App() {
  return (
    <div className="App">
      <FunnelChart title="xgqfrms">
        <Slot />
      </FunnelChart>
      <hr />
      <FunnelChart
        title="xgqfrms"
        template={title => <Slot title={title}/>}
        slot={<Slot />}
      />
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);


import React from "react";

const FunnelChart = (props) => {
  const {
    title,
    children,
    slot,
    template,
  } = props;
  console.log(`template`, template);
  return(
    <>
      <span>slot parent component</span>
      <div>
        {
          children
        }
        {
          slot
        }
        <br />
        {
          template && template(title)
        }
        {/* {
          <children title={title} />
        } */}
        {/* {
          slot(title)
        } */}
      </div>
    </>
  );
};

export {
  FunnelChart,
};

export default FunnelChart;


import React from "react";

const Slot = (props) => {
  const {
    title,
  } = props;
  const color = `${title ? "green" : "red"}`;
  return(
    <>
      <span style={{color}}>{title ? title : `default title`}</span>
    </>
  );
};

export {
  Slot,
};

export default Slot;

props.children

https://reactjs.org/docs/composition-vs-inheritance.html


function Dialog(props) {
  return (
    <FancyBorder color="blue">
      <h1 className="Dialog-title">
        {props.title}
      </h1>
      <p className="Dialog-message">
        {props.message}
      </p>
      {props.children}
    </FancyBorder>
  );
}

class SignUpDialog extends React.Component {
  constructor(props) {
    super(props);
    this.handleChange = this.handleChange.bind(this);
    this.handleSignUp = this.handleSignUp.bind(this);
    this.state = {login: ''};
  }

  render() {
    return (
      <Dialog title="Mars Exploration Program"
              message="How should we refer to you?">
        <input value={this.state.login}
               onChange={this.handleChange} />

        <button onClick={this.handleSignUp}>
          Sign Me Up!
        </button>
      </Dialog>
    );
  }

  handleChange(e) {
    this.setState({login: e.target.value});
  }

  handleSignUp() {
    alert(`Welcome aboard, ${this.state.login}!`);
  }
}

How to pass data to props.children

https://frontarm.com/james-k-nelson/passing-data-props-children/

How to pass props to

https://stackoverflow.com/questions/32370994/how-to-pass-props-to-this-props-children

how-to-pass-props-from-child-to-parent-component

https://www.robinwieruch.de/react-pass-props-to-component#how-to-pass-props-from-child-to-parent-component


https://medium.com/better-programming/passing-data-to-props-children-in-react-5399baea0356

vue slot

https://www.cnblogs.com/xgqfrms/p/11218372.html

Web Components & HTML template & HTML slot

https://www.cnblogs.com/xgqfrms/p/10979925.html

refs



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @   xgqfrms  阅读(283)  评论(7编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2018-10-24 UI & 原型
2018-10-24 docsearch & algolia
2015-10-24 Zach Holman on all Things Github
2015-10-24 MOOC学习成果认证及对高等教育变革路径的影响
2015-10-24 memcached php
2015-10-24 Nginx环境下,PHP下载,中文文件,下载失效(英文可以下载)怎么解决呢?
点击右上角即可分享
微信分享提示