[英]How to pass class component as a functional prop in react?
Trying to send component as a prop using lambda function and TSLint throws exception.尝试使用 lambda 函数和 TSLint 将组件作为道具发送会引发异常。
A component can be sent as a prop like this:一个组件可以作为一个 prop 发送,如下所示:
<Test
id={'XYZ-1809'}
condn1={<Condn1Component />}
condn2={<Condn2Component />}
/>
But when trying to send it as a functional prop, throws an error as: Lambdas are forbidden in JSX attributes due to their rendering performance impact (jsx-no-lambda)tslint(1)
但是当尝试将其作为功能道具发送时,会引发错误:
Lambdas are forbidden in JSX attributes due to their rendering performance impact (jsx-no-lambda)tslint(1)
<Test
id={'XYZ-1809'}
condn1={() => <Condn1Component />}
condn2={() => <Condn2Component />}
/>
Condn1Component
and Condn2Component
can be a functional or class component that is unsure. Condn1Component
和Condn2Component
可以是不确定的功能或类组件。
What's the best possible way to get rid of JSX-Lambda issue?摆脱 JSX-Lambda 问题的最佳方法是什么?
I managed to do this like below:我设法做到这一点如下:
const Condn1Component = () => {
return (<h1>I am condition1</h1>);
}
const Condn2Component = () => {
return (<h1>I am condition2</h1>);
}
const condn1ComponentHandler = () => {
return <Condn1Component />
}
const condn2ComponentHandler = () => {
return <Condn2Component />
}
<Test
id={'XYZ-1809'}
condn1={condn1ComponentHandler}
condn2={condn2ComponentHandler}
/>
And accepted as:并接受为:
this.props.condn1();
this.props.condn2();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.