简体   繁体   English

如何在反应中将类组件作为功能道具传递?

[英]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. Condn1ComponentCondn2Component可以是不确定的功能或类组件。

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.

相关问题 如何将 function 从 FUNCTIONAL 传递给 CLASS 组件并在渲染外部访问它(没有 prop ),在反应 js 中使用上下文? - How to pass function from FUNCTIONAL to CLASS component and access it outside of render( without prop ), using context in react js? 如何将React组件作为prop传递 - How to pass a React component as a prop React 功能组件中的 Prop 名称 - Prop name in functional component in React 如何将功能组件中的道具添加到 class 组件中? - How to add a prop from a functional component into a class component? React:如何从组件传递宽度作为支撑 - React: How to pass width as a prop from the component 如何将 React Router 位置道具传递给组件? - How to pass the React Router location prop to a component? 如何动态决定将prop传递给反应组件? - How to dynamically decide to pass a prop to a react component or not? React - 如何将道具传递给作为道具传递的组件 - React - How to pass props to a component passed as prop React 如何将道具发送到 HOC 内部使用的功能组件? - React How to send a prop to a functional component being used inside of a HOC? 如何在React的功能组件中将参数传递给功能 - How to pass arguments to function in functional component in React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM