admin管理员组文章数量:1300004
I'm having a problem when using forwardRef
with TypeScript.
What I want? pass a typed ref to the child ponent.
What am I getting
value of console.log - child ponent null
Code for parent ponent
// ponent parent
import { FormHandles } from '@unform/core';
export const App = () => {
const formRef = createRef<FormHandles>();
return (
<Child ref={formRef} />
)
}
Code for child ponent
// ponent child
import { FormHandles } from '@unform/core';
export const Child = forwardRef<FormHandles>((props, ref) => {
console.log('child ponent', ref.current);
return (
<div>any thing</div>
)
})
I'm having a problem when using forwardRef
with TypeScript.
What I want? pass a typed ref to the child ponent.
What am I getting
value of console.log - child ponent null
Code for parent ponent
// ponent parent
import { FormHandles } from '@unform/core';
export const App = () => {
const formRef = createRef<FormHandles>();
return (
<Child ref={formRef} />
)
}
Code for child ponent
// ponent child
import { FormHandles } from '@unform/core';
export const Child = forwardRef<FormHandles>((props, ref) => {
console.log('child ponent', ref.current);
return (
<div>any thing</div>
)
})
Share
Improve this question
asked May 28, 2021 at 13:23
AndresdoSantosAndresdoSantos
591 gold badge1 silver badge5 bronze badges
1
-
Use
useRef
hook inApp
to create (and use i.e. access that was already created) the ref. Why is itnull
? It depends on your code. It might also beundefined
because you gave it no initial value increateRef<FormHandles>();
so, from where should it get the value? – Ajeet Shah Commented May 28, 2021 at 13:48
2 Answers
Reset to default 4import React, {createRef, forwardRef} from 'react';
type Props = { children: React.ReactNode; type: "submit" | "button" };
type Ref = HTMLButtonElement;
const Child = React.forwardRef<Ref, Props>((props, ref) => {
console.log(ref);
return (<button ref={ref} className="MyChild" type={props.type}>{props.children}</button>);
});
function App() {
const ref = React.createRef<HTMLButtonElement>();
return (
<Child type="button" ref={ref}>Click Me</Child>
)
}
export default App;
This is an example to show you how to use forwardRef with typescript
import React, { ChangeEventHandler } from 'react';
import Button from '../button';
import TextInput from '../text-input';
import SearchIcon from '../../icons/Search';
export interface SearchProps {
className?: string;
size?: ComponentSize;
width?: string;
value?: string;
onChange?: ChangeEventHandler<HTMLInputElement>;
placeholder?: string;
}
const Search: React.ForwardRefRenderFunction<HTMLDivElement, SearchProps> = (props, ref) => {
const {
size = 'default',
className,
value,
onChange,
placeholder,
width = '100%',
} = props;
return (
<div
ref={ref}
className={className}
width={width}
>
<TextInput
value={value}
onChange={onChange}
placeholder={placeholder}
clearable
/>
<Button type='secondary' icon={SearchIcon} />
</div>
);
}
export default React.forwardRef<HTMLDivElement, SearchProps>(Search);
本文标签: javascriptHow to use TypeScript and forwardRef in ReactJSStack Overflow
版权声明:本文标题:javascript - How to use TypeScript and forwardRef in ReactJS? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741656249a2390777.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论