- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我正在尝试使用 React.forwardRef,但对如何让它在基于类的组件(而非 HOC)中工作感到困惑。
文档示例使用元素和功能组件,甚至将类包装在高阶组件的函数中。
因此,从类似 this 的内容开始在他们的 ref.js
文件中:
const TextInput = React.forwardRef(
(props, ref) => (<input type="text" placeholder="Hello World" ref={ref} />)
);
而是将其定义为如下内容:
class TextInput extends React.Component {
render() {
let { props, ref } = React.forwardRef((props, ref) => ({ props, ref }));
return <input type="text" placeholder="Hello World" ref={ref} />;
}
}
或
class TextInput extends React.Component {
render() {
return (
React.forwardRef((props, ref) => (<input type="text" placeholder="Hello World" ref={ref} />))
);
}
}
只工作:/
此外,我知道我知道,ref 不是 react 方式。我正在尝试使用第三方 Canvas 库,并想在单独的组件中添加他们的一些工具,所以我需要事件监听器,所以我需要生命周期方法。以后可能会走不同的路线,但我想试试这个。
文档说这是可能的!
Ref forwarding is not limited to DOM components. You can forward refs to class component instances, too.
但随后他们继续使用 HOC 而不仅仅是类。
最佳答案
始终对 ref
使用相同属性的想法可以通过使用助手代理类导出来实现。
class ElemComponent extends Component {
render() {
return (
<div ref={this.props.innerRef}>
Div has a ref
</div>
)
}
}
export default React.forwardRef((props, ref) => <ElemComponent
innerRef={ref} {...props}
/>);
所以基本上,我们被迫使用不同的 prop 来转发 ref,但这可以在 hub 下完成。重要的是公众将其用作正常引用。
关于javascript - 如何在基于类的组件中使用 React.forwardRef?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51526461/
我使用的技术是 react、typescript 和 styled-components。我正在尝试创建选择组件以便在 React Hook Form 中使用它。起初,看起来一切都是正确的,但我从 t
什么是forwardRef用angular做的,它的用法是什么? 这是一个 example : import {Component, Injectable, forwardRef} from '@an
我买了一个 Angular 4 模板,主要是为了看看布局是如何完成的,但包括工作组件,我注意到他们在应用程序组件中有一堆设置,主要用于菜单、更改样式、主要控制外部框架. 在菜单组件和其他一些组件中,它
这是我的带有 forwardRef 的自定义组件。 ref 在组件内部代码中是必需的,但父组件将其作为 prop 发送是可选的 const MyComponent = React.forwardRef
@Component({ selector: 'my-component', template: ``, providers: [ { provide: SourceCompone
TL;DR:我在库中有一个功能组件,它具有 react@^16.3.0 作为对等依赖项。如果我将此组件包装在 React.forwardRef 中,这是否是重大更改? 根据 forwarding Re
我当前正在运行 React 16.3.1 和 Styled Components 3.2.5,并且在尝试使用 React.forwardRef 时遇到问题。 我有一个输入组件,它由一个包含标签和输入字
我正在将我的应用程序移动到 Material UI V4,并且当以编程方式设置“to”属性时,我正在努力将我的 react 路由器链接组件移动到 forwardRef 包装的组件中。 下面的代码有效,
我不明白这是什么意思: const FancyButton = React.forwardRef((props, ref) => ( {props.children} )); 如果
我正在尝试使用 React.forwardRef,但对如何让它在基于类的组件(而非 HOC)中工作感到困惑。 文档示例使用元素和功能组件,甚至将类包装在高阶组件的函数中。 因此,从类似 this 的内
我有一个用 React.forwardRef 包装的组件,碰巧它也是一个复合组件。 我不知道该如何保养 Form.Item = FormItem; 同时 Form 组件函数被 forwardRef
父组件: const Parent = (props) => { const ref = useRef(); return } 和 child : const Child = forward
我已经使用 React 转发了 ref forwardRef 下面是我的代码: interface PropsDummy {} const ProfileMenu = forwardRef((prop
我需要访问组件内的文本区域的引用。在组件中,它很简单: const MyComponent = () => { const inputRef = useRef(); return } 现在
我正在尝试编写两个 React 样式的组件(使用 Emotion 10),将一些自定义渲染逻辑指定到其中一个中,并保留检索它们的“引用”的能力。 这是我的代码: const Foo = styled(
我对 React.forwardRef 的观点感到困惑.正如其 documentation 中所述,据我所知,它的主要用途是用于 父组件 访问 DOM 的元素子组件 .但我已经可以做到这一点,甚至不必
我收到警告 Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did
我有一个属于 UI 库的组件,我们称它为输入组件。使用这个库调用Input时,我可以调用的类型有很多。例如 现在我想给这个Input组件写一个wrapper,所以我这样写 type InputW
我需要使用 ref获取孙组件的状态,我使用 useImperativeHandle 对其进行了自定义,简化了整个代码 here . function App() { const ref = use
我目前在我的 React 应用程序中收到以下错误: Function components cannot be given refs. Attempts to access this refwill
我是一名优秀的程序员,十分优秀!