Skip to content

Commit 8ccedc7

Browse files
committed
fix: memoize styles/colors
1 parent 0bb47cb commit 8ccedc7

3 files changed

Lines changed: 57 additions & 56 deletions

File tree

exampleapp/App.js

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,7 @@ export default class App extends Component {
312312
selectChildren: false,
313313
hideChipRemove: false,
314314
hasErrored: false,
315-
isDarkMode: false,
316-
selectToggleTextColor: 'black'
315+
isDarkMode: false
317316
}
318317
this.termId = 100
319318
this.maxItems = 5
@@ -714,10 +713,7 @@ export default class App extends Component {
714713
selectedItems={this.state.selectedItems}
715714
colors={{
716715
primary: '#5c3a9e',
717-
success: '#bada55',
718-
subItemBackground:
719-
this.state.selectedItems.length > 2 ? 'purple' : 'gold',
720-
selectToggleTextColor: this.state.selectToggleTextColor,
716+
success: '#5c3a9e',
721717
chipColor: this.state.isDarkMode ? '#f7f7f7' : '#333'
722718
}}
723719
itemNumberOfLines={3}
@@ -797,20 +793,7 @@ export default class App extends Component {
797793
onPress={() => this.onSwitchToggle('hideChipRemove')}
798794
val={this.state.hideChipRemove}
799795
/>
800-
<TouchableWithoutFeedback
801-
onPress={() => this.setState({ selectToggleTextColor: 'pink' })}
802-
>
803-
<View style={styles.switch}>
804-
<Text style={styles.label}>Color test</Text>
805-
</View>
806-
</TouchableWithoutFeedback>
807-
<TouchableWithoutFeedback
808-
onPress={() => this.setState({ selectToggleTextColor: 'green' })}
809-
>
810-
<View style={styles.switch}>
811-
<Text style={styles.label}>Color test</Text>
812-
</View>
813-
</TouchableWithoutFeedback>
796+
814797
<TouchableWithoutFeedback
815798
onPress={() => this.SectionedMultiSelect._removeAllItems()}
816799
>

lib/components/RowSubItem.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { Component } from 'react'
2+
import isEqual from 'lodash.isequal'
23
import { View, TouchableOpacity, Text } from 'react-native'
34
import ItemIcon from './ItemIcon'
45
import { callIfFunction } from '../helpers'
@@ -43,7 +44,10 @@ class RowSubItem extends Component {
4344
}
4445
}
4546
}
46-
if (this.props.mergedStyles !== nextProps.mergedStyles) {
47+
if (!isEqual(this.props.mergedStyles, nextProps.mergedStyles)) {
48+
return true
49+
}
50+
if (!isEqual(this.props.mergedColors, nextProps.mergedColors)) {
4751
return true
4852
}
4953
return false

lib/sectioned-multi-select.js

Lines changed: 49 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
ActivityIndicator
1616
} from 'react-native'
1717
import isEqual from 'lodash.isequal'
18+
import memoize from 'memoize-one'
1819
import { RowItem } from './components'
1920
import { callIfFunction } from './helpers'
2021

@@ -322,24 +323,18 @@ class SectionedMultiSelect extends PureComponent {
322323
this.state = {
323324
selector: false,
324325
searchTerm: '',
325-
highlightedChildren: [],
326-
styles: StyleSheet.flatten([defaultStyles, props.styles]),
327-
colors: StyleSheet.flatten([defaultColors, props.colors])
326+
highlightedChildren: []
328327
}
329328
}
330329

331-
componentDidUpdate(prevProps) {
332-
if (!isEqual(prevProps.styles, this.props.styles)) {
333-
this.setState({
334-
styles: StyleSheet.flatten([defaultStyles, this.props.styles])
335-
})
336-
}
337-
if (!isEqual(prevProps.colors, this.props.colors)) {
338-
this.setState({
339-
colors: StyleSheet.flatten([defaultColors, this.props.colors])
340-
})
341-
}
342-
}
330+
getStyles = memoize(
331+
(styles) => StyleSheet.flatten([defaultStyles, styles]),
332+
isEqual
333+
)
334+
getColors = memoize(
335+
(colors) => StyleSheet.flatten([defaultColors, colors]),
336+
isEqual
337+
)
343338

344339
// componentWillUpdate() { date = new Date();}
345340
// componentDidUpdate() {console.log(new Date().valueOf() - date.valueOf())}
@@ -390,7 +385,10 @@ class SectionedMultiSelect extends PureComponent {
390385
renderSelectText,
391386
selectLabelNumberOfLines
392387
} = this.props
393-
const { colors, styles } = this.state
388+
389+
const styles = this.getStyles(this.props.styles)
390+
const colors = this.getColors(this.props.colors)
391+
394392
let customSelect = null
395393

396394
if (renderSelectText) {
@@ -789,7 +787,9 @@ class SectionedMultiSelect extends PureComponent {
789787
}
790788

791789
_customChipsRenderer = () => {
792-
const { styles, colors } = this.state
790+
const styles = this.getStyles(this.props.styles)
791+
const colors = this.getColors(this.props.colors)
792+
793793
const {
794794
displayKey,
795795
items,
@@ -814,7 +814,9 @@ class SectionedMultiSelect extends PureComponent {
814814
}
815815

816816
_renderChips = () => {
817-
const { styles, colors } = this.state
817+
const styles = this.getStyles(this.props.styles)
818+
const colors = this.getColors(this.props.colors)
819+
818820
const {
819821
selectedItems,
820822
single,
@@ -900,7 +902,9 @@ class SectionedMultiSelect extends PureComponent {
900902
hideChipRemove,
901903
IconRenderer
902904
} = this.props
903-
const { styles, colors } = this.state
905+
const styles = this.getStyles(this.props.styles)
906+
const colors = this.getColors(this.props.colors)
907+
904908
return selectedItems.map((singleSelectedItem) => {
905909
const item = this._findItem(singleSelectedItem)
906910

@@ -979,27 +983,33 @@ class SectionedMultiSelect extends PureComponent {
979983
})
980984
}
981985

982-
_renderSeparator = () => (
983-
<View
984-
style={[
985-
{
986-
flex: 1,
987-
height: StyleSheet.hairlineWidth,
988-
alignSelf: 'stretch',
989-
backgroundColor: '#dadada'
990-
},
991-
this.state.styles.separator
992-
]}
993-
/>
994-
)
986+
_renderSeparator = () => {
987+
const styles = this.getStyles(this.props.styles)
988+
return (
989+
<View
990+
style={[
991+
{
992+
flex: 1,
993+
height: StyleSheet.hairlineWidth,
994+
alignSelf: 'stretch',
995+
backgroundColor: '#dadada'
996+
},
997+
styles.separator
998+
]}
999+
/>
1000+
)
1001+
}
9951002

9961003
_renderFooter = () => {
9971004
const { footerComponent } = this.props
9981005
return <View>{footerComponent && callIfFunction(footerComponent)}</View>
9991006
}
10001007

10011008
_renderItemFlatList = ({ item }) => {
1002-
const { styles, colors } = this.state
1009+
const styles = this.getStyles(this.props.styles)
1010+
const colors = this.getColors(this.props.colors)
1011+
1012+
// const { styles, colors } = this.state
10031013

10041014
const { searchTerm } = this.state
10051015
return (
@@ -1020,7 +1030,8 @@ class SectionedMultiSelect extends PureComponent {
10201030
}
10211031
ModalContentWrapper = ({ children }) => {
10221032
const { modalWithSafeAreaView } = this.props
1023-
const { styles } = this.state
1033+
const styles = this.getStyles(this.props.styles)
1034+
10241035
const Component = modalWithSafeAreaView ? SafeAreaView : View
10251036
return (
10261037
<Component
@@ -1088,7 +1099,10 @@ class SectionedMultiSelect extends PureComponent {
10881099
disabled,
10891100
itemsFlatListProps
10901101
} = this.props
1091-
const { searchTerm, selector, styles, colors } = this.state
1102+
const { searchTerm, selector } = this.state
1103+
const styles = this.getStyles(this.props.styles)
1104+
const colors = this.getColors(this.props.colors)
1105+
console.log({ styles, colors })
10921106
const renderItems = searchTerm
10931107
? this._filterItems(searchTerm.trim())
10941108
: items

0 commit comments

Comments
 (0)