Tuesday, May 21, 2024
 Popular · Latest · Hot · Upcoming
151
rated 0 times [  157] [ 6]  / answers: 1 / hits: 11548  / 4 Years ago, wed, september 16, 2020, 12:00:00

I am learning how to use forward refs here I have a FC where I need to initalize all my refs and then pass them down to it's children so I can get the canvas instances of some chartjs charts. However using forwardRef I get a type error saying ref is not a property of the child.


const BUIDashboard: React.FC = () => {
const chartRef = React.createRef<RefObject<HorizontalBar>>()
.
.
.
return (

<Child
ref={chartRef} <------------------- TYPE ERROR HERE
isLoading={isLoadingChild}
data={childData} />
)
}

There are no errors in the child but it's set up like this


type Props = {
rootProps?: DashboardCardProps
isLoading?: boolean
data?: BUIMetrics['breachBreakdownByOutcome']
}

const Child: React.FC<Props> = React.forwardRef(({ rootProps, isLoading, data }, ref: RefObject<HorizontalBar>) => {

return (
<HorizontalBar ref={ref}/>
)
}

Have I defined the params for the child incorrectly?


I thought the issue might be this line


const Child: React.FC<Props>


so I updated the Props type to


type Props = {
rootProps?: DashboardCardProps
isLoading?: boolean
data?: BUIMetrics['breachBreakdownByOutcome']
} & { ref: RefObject<HorizontalBar> }


However then the Child component declaration throws this error:


TS2322: Type 'ForwardRefExoticComponent<Pick<Props, "rootProps" | "isLoading" | "data"> & RefAttributes<HorizontalBar>>' is not assignable to type 'FC<Props>'.
Types of property 'defaultProps' are incompatible.
Type 'Partial<Pick<Props, "rootProps" | "isLoading" | "data"> & RefAttributes<HorizontalBar>> | undefined' is not assignable to type 'Partial<Props> | undefined'.
Type 'Partial<Pick<Props, "rootProps" | "isLoading" | "data"> & RefAttributes<HorizontalBar>>' is not assignable to type 'Partial<Props>'.
Types of property 'ref' are incompatible.
Type '((instance: HorizontalBar | null) => void) | RefObject<HorizontalBar> | null | undefined' is not assignable to type 'RefObject<HorizontalBar> | undefined'.
Type 'null' is not assignable to type 'RefObject<HorizontalBar> | undefined'.


Also this is blatant advertising but I am trying to figure this out to resolve another issue linked below. If you have any insight on this problem please let me know as well. Thank You.
Using react-pdf with react-chartjs-2 to generate a pdf


More From » reactjs

 Answers
0
type Props = {
rootProps?: DashboardCardProps
isLoading?: boolean
data?: BUIMetrics['breachBreakdownByOutcome']
}

const Child = React.forwardRef<RefObject<HorizontalBar>,Props>(({ rootProps, isLoading, data, children }, ref) => {
return (
<HorizontalBar ref={ref}/>
);
})

[#2669] Saturday, September 12, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kaleyv

Total Points: 259
Total Questions: 99
Total Answers: 107

Location: Saint Helena
Member since Tue, Nov 3, 2020
4 Years ago
kaleyv questions
Thu, Dec 16, 21, 00:00, 2 Years ago
Sat, Oct 24, 20, 00:00, 4 Years ago
Sun, Oct 18, 20, 00:00, 4 Years ago
;