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
asFunction 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://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, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/11730356.html
未经授权禁止转载,违者必究!