reactjs - 在组件 B 中使用组件 A 的返回值
问题描述
我有以下组件。
//Component A
import * as React from "react";
interface IProps {
length: number;
width: number;
}
export default function Test(props: IProps) {
const { length, width } = props;
const someNumber = 12;
let returnNum: number = 0;
returnNum = (length * width );
return <> { returnNum } </>;
}
我想返回returnNum
到以下组件
// Component B
import * as React from "react";
interface IProps {
returnNum: number;
}
export default function GetValueToThisFunction(props: IProps) {
const { returnNum } = props;
let valueRet = 0;
if (returnNum < 1) {
valueRet = 400;
} else if (returnNum >= 1 && returnNum < 2) {
valueRet = 300;
}
}
我正在通过以下方式使用我的组件
<Test length={18} width={3}/>
<GetValueToThisFunction returnNum={} />;
我基本上想传递Test
组件返回值(returnNum)并将其用于GetValueForThisFunction
解决方案
您可以使用更高阶的组件来呈现您的组件。
const withArea = Component => ({ length, width }) => (
<Fragment>
{/* render whatever you want here */}
<Component area={length * width} />
</Fragment>
);
const ComponentWithArea = withArea(MyComponent);
const App = () => (
<ComponentWithArea length={5} width={10} />
)
推荐阅读
- ios - 如何在呈现新的 UITabBarController 之前关闭 rootViewController?
- excel - Excel 2010 VBA:如何将一行粘贴到一系列行并保持逐行条件格式
- python - 在两个日期之间提取已保存日期的文件
- regex - 如何从下拉菜单中查询多列和某一列的计数
- r - R:当点重叠/在距离内时追加数据;将缓冲区矩形添加到 set1,将半径添加到 set2
- api - 如何通过 API 为 Microsoft 订阅购买更多许可证
- gnuplot - Gnuplot,直方图中的条形重叠,不能分开,为什么
- javascript - 如何从 textarea 标签中获取值并将它们全部放入数组中?
- typescript - Typescript 泛型数组被强制转换为 any[]?
- go - 用指针序列化结构