1
- import React , { useRef , useMemo , memo , useState , useEffect } from "react"
1
+ import React , { useRef , useMemo } from "react"
2
2
import styled from "styled-components"
3
3
import ReactSelect , { components as defaultComponents } from "react-select"
4
4
import Creatable from "react-select/creatable"
@@ -84,7 +84,7 @@ const customComponents = {
84
84
ValueContainer : withDataAttrs ( defaultComponents . ValueContainer , "ValueContainer" ) ,
85
85
}
86
86
87
- const VirtualItem = memo ( ( { virtualRow, child, measureElement } ) => (
87
+ const VirtualItem = ( { virtualRow, child } ) => (
88
88
< div
89
89
key = { virtualRow . key }
90
90
style = { {
@@ -95,27 +95,21 @@ const VirtualItem = memo(({ virtualRow, child, measureElement }) => (
95
95
position : "absolute" ,
96
96
} }
97
97
data-index = { virtualRow . index }
98
- ref = { measureElement }
99
98
>
100
99
{ child }
101
100
</ div >
102
- ) )
101
+ )
103
102
104
- const VirtualList = memo ( ( { optionsLength , children, parentRef, setIsMounted } ) => {
103
+ const VirtualList = ( { children, parentRef } ) => {
105
104
const virtualizer = useVirtualizer ( {
106
- count : optionsLength ,
105
+ count : children . length ,
107
106
getScrollElement : ( ) => parentRef . current ,
108
- estimateSize : ( ) => 20 ,
107
+ estimateSize : ( ) => 35 ,
109
108
overscan : 5 ,
110
109
} )
111
110
112
111
const virtualItems = virtualizer . getVirtualItems ( )
113
112
114
- useEffect ( ( ) => {
115
- const timeoutId = setTimeout ( setIsMounted ( true ) , 0 )
116
- return ( ) => clearTimeout ( timeoutId )
117
- } , [ setIsMounted ] )
118
-
119
113
return (
120
114
< div
121
115
style = { {
@@ -128,15 +122,13 @@ const VirtualList = memo(({ optionsLength, children, parentRef, setIsMounted })
128
122
key = { virtualRow . key }
129
123
virtualRow = { virtualRow }
130
124
child = { children [ virtualRow . index ] }
131
- measureElement = { virtualizer . measureElement }
132
125
/>
133
126
) ) }
134
127
</ div >
135
128
)
136
- } )
129
+ }
137
130
138
131
const VirtualizedMenuList = props => {
139
- const [ isMounted , setIsMounted ] = useState ( false )
140
132
const parentRef = useRef ( )
141
133
142
134
return (
@@ -149,14 +141,7 @@ const VirtualizedMenuList = props => {
149
141
position : "relative" ,
150
142
} }
151
143
>
152
- < VirtualList
153
- parentRef = { parentRef }
154
- optionsLength = { props . options . length }
155
- isMounted = { isMounted }
156
- setIsMounted = { setIsMounted }
157
- >
158
- { props . children }
159
- </ VirtualList >
144
+ < VirtualList parentRef = { parentRef } > { props . children } </ VirtualList >
160
145
</ customComponents . MenuList >
161
146
)
162
147
}
0 commit comments