I was trying to use defaultProps in functional component along with forwardRef, but was getting the error (refer screenshot).
I don't want to destructure the props in function params and assign default value. because got a lot of props in my original usecase.
I also tried assigning the default values JSON directly to ComponentA.defaultProps still same result.
How to resolve this ? Tried surfing but was not able to find any solution.
Also since we are using TS for type check, do we still need prototype JSON ?
#App.tsx
import './App.css';
import ComponentA from './components/A/A';
function App() {
return (
<div className="App">
<ComponentA>
<button>Button I am</button>
</ComponentA>
</div>
);
}
export default App;
#ComponentA.tsx
import React,{forwardRef} from "react"
import PropTypes from 'prop-types';
type Props = {
children:JSX.Element | JSX.Element[],
text:string,
text1?:string,
}
const ComponentA=(prop:Props)=>{
return <div>Hey
{prop.children}
{prop.text}
{prop.text1}
</div>
}
ComponentA.prototype = {
children:PropTypes.element.isRequired,
text:PropTypes.string.isRequired,
text1:PropTypes.string
}
ComponentA.defaultProps={
text: 'done'
} as Partial<Props>
export default forwardRef<HTMLAllCollection,Props>(ComponentA)
Thanks