Answer the specific question described in the original post
In the example with the question about OP, it is necessary to declare not the support type ref, but the material referred to by the link, and which will be transferred from Redx using mapStateToProps
. The type of support for the DOM element will be: myRefToBePutInReduxGlobalStore: PropTypes.instanceOf(Element)
(if it is a DOM element). Although I would:
- Rename the statics to
myElementToBePutInReduxGlobalStore
(this is not a link as such) - , React
: React?
:
function FancyInput({ inputRef }) {
return (
<div className="fancy">
Fancy input
<input ref={inputRef} />
</div>
)
}
FancyInput.propTypes = {
inputRef: ??? // What is the correct prop type here?
}
function App() {
const inputRef = React.useRef()
useEffect(function focusWhenStuffChange() => {
inputRef.current && inputRef.current.focus()
}, [stuff])
return <FancyInput inputRef={inputRef} />
}
:
- , :
{ current: [something] }
,
React.createRef()
React.useRef()
- ,
[something]
( OP)
: ref,
: PropTypes.func
.
, , .
TL; DR
DOM, div
input
, :
refProp: PropTypes.oneOfType([
PropTypes.func,
PropTypes.shape({ current: PropTypes.instanceOf(Element) })
])
ref
- , DOM.
:
refProp: PropTypes.oneOfType([
PropTypes.func,
PropTypes.shape({ current: PropTypes.any })
])
ref current
. . , , , , . , { current: [any] }
refToForward
, .
, , - , , , .
, , , . .
, , - HTML Element
:
refProp: PropTypes.oneOfType([
PropTypes.func,
PropTypes.shape({ current: PropTypes.instanceOf(HTMLInputElement) })
])
, React:
refProp: PropTypes.oneOfType([
PropTypes.func,
PropTypes.shape({ current: PropTypes.instanceOf(Component) })
])
, , useImperativeHandle
, - :
refProp: PropTypes.oneOfType([
PropTypes.func,
PropTypes.shape({ current: PropTypes.object })
])
: prop , DOM, JavaScript Object
, . - , , . .
, DOM-, Element
undefined
NodeJS. :
Element = typeof Element === 'undefined' ? function(){} : Element
React Docs , , , useRef
@Rahul Sagore, @Ferenk Kamras @svnm