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 @ 2019-10-24 09:29  xgqfrms  阅读(282)  评论(7编辑  收藏  举报