javascript - 如何使用 React.forwardRef(); 和 JSX 的点表示法
问题描述
我尝试导出为默认组件,但出现错误。我在这里也遇到了类似的问题,但我不懂打字稿语法
应用输入组件
const CustomInput = {
AppInput: function AppInput(props) {
return <TextField id={id} {...props} />
},
// Another method exported as a named import
MetricInput: function MetricInput(props) {
return (
<FormControl className="form-control">
<Input {...props} />
</FormControl>
);
}
};
export function MetricInput(props) {
return <CustomInput.MetricInput {...props} />;
};
function AppInput(props, ref) {
props = { ...props, ref };
return <CustomInput.AppInput {...props} />;
};
const InputEL = React.forwardRef(AppInput);
带导入的组件
import InputEl from "../common/CustomInput/";
//! ref from the root component was passed successfully
const DrugDosage = ({ handleKeydown}, ref) => {
return(
<InputEl
ref={ref}
name="dsgStatem"
className="col-12"
onKeyDown={handleKeydown}
id="drug_dsg_long"
required
/>
)
}
const DrugDosageRef = React.forwardRef(DrugDosage);
export default DrugDosageRef;
解决方案
推荐阅读
- web-scraping - 如何在scrapy数据提取中获得多用户登录?
- split - 我想拆分和重命名大量文件。我的代码有错误
- php - 如何在 PHP 中四舍五入到最近的 15 分钟
- sql-server - SQL查询获取最后一条记录按多个字段分组
- qt - QtQuick Grid,鼠标事件不会传播到相邻的鼠标区域
- java - Android Java Webview 操作 XHR/AJAX-Request
- php - WordPress:从父类别获取子类别
- css - 多个“子应用程序”合二为一(CSS 冲突)
- mysql - 如何通过分组计算多行的百分比
- sql-server - 我可以在不指定表名的情况下从多个 SQL Server 表中进行选择吗