[英]Typescript + React - Passing/Exploding Props in JSX to pass to children?
after some pain I've realised I don't actually understand how this works or really what its called在经历了一些痛苦之后,我意识到我实际上并不理解它是如何工作的,也不知道它到底叫什么
Foreward: New to typescript/javascript and react前言:打字稿/javascript 和反应的新手
I'm using Formik and trying to create some custom functions to make it easier for my team, and I've found with Formik I can do the following (Tutorials are great):我正在使用 Formik 并尝试创建一些自定义函数以使我的团队更轻松,并且我发现使用 Formik 我可以执行以下操作(教程很棒):
<Formik
{...props}
validationSchema={validationSchema}
onSubmit={async (values, helpers) => {
await props.onSubmit(values, helpers);
setCompleted(true);
}}
>
{({ isSubmitting, setFieldValue, touched, errors }) => (
...
In this I'm now able to use touched, errors etc. from Formik (From what I understand?)在这里,我现在可以使用 Formik 的 touched、errors 等(据我了解?)
How would I do this with my own custom functions/components?我将如何使用我自己的自定义函数/组件来执行此操作?
Ie IE
// In MyComponent
const myCoolConst = "George"
<MyComponent name="Hello" > {(myCoolConst)} => {
<text>{myCoolConst}</text>
}
</>
Also bonus from me, but is what I'm trying to do considered an anti-pattern or something?也是我的奖励,但是我正在尝试做的事情被认为是反模式还是什么?
This is the render props paradigm and here is a simple example of how to go about it,这是渲染道具范例,这里是一个简单的例子,说明如何 go 关于它,
const MyComponent = ({children}) => {
return (
<>
<div>Content from MyComponent</div>
<>{children(1, 2)}</>
</>
);
};
export default function App() {
return (
<MyComponent>
{(a, b) => {
return <>
<p>{a}</p>
<p>{b}</p>
</>
}}
</MyComponent>
);
}
Here the children
prop is a special kind of prop, which is automatically available to you to have content rendered via callbacks.这里的
children
道具是一种特殊的道具,您可以自动使用它来通过回调呈现内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.