javascript - 颜色在 sectionList 上保持随机化

标签 javascript reactjs react-native redux native

您好,我有一个 sectionList,它传递了一组颜色 + 随机选择的背景 img,以呈现为每个行项目的背景。当我滑动访问该 View 时,颜色会闪烁几秒钟,因为随机选择仍在发生。每次我返回带有列表的屏幕时,颜色都会闪烁几秒钟然后稳定下来。我怎样才能没有闪烁,并且随机选择颜色会在加载时发生一次?

            class Main extends React.Component {
                constructor(props, context) {
                super(props, context);

                // app state
                this.state = {
                    listColor: [
                    ['rgba(0,36,155,0.8)', 'rgba(26,0,87,0.8)'],
                    ['rgba(155,0,0,0.8)', 'rgba(87,0,0,0.8)']],
                }
                }


            _handleRandomIndex(arr) {
                return arr[Math.floor(Math.random() * arr.length)]
            }

            _renderItem = ({item, section}) => (
                <TouchableScale
                        style={styles.row}
                        onPress={this._onRowPress.bind(this, item)}
                        activeScale={0.94}
                        tension={0}
                        friction={3}
                        >
                        <View style={ styles.elevationLow } borderRadius={9} > 
                            <ImageBackground source={{uri: this._handleRandomIndex(item.bgImgs).image_link }} borderRadius={9} style={ styles.imageBackground }>
                                <LinearGradient
                                        colors={ this._handleRandomIndex(this.state.listColor) }
                                        start={[0.1,0.1]}
                                        end={[0.5,0.5]}
                                        style={{ padding: 20, borderRadius: 9 }}>

                                </LinearGradient>
                            </ImageBackground>
                        </View>  
                        }
                </TouchableScale>
            )
            }

最佳答案

因为每次重新渲染时您都在执行 Math.random。所以每当渲染函数调用时,它都会改变颜色。

更改为:

function _handleRandomIndex(arr) {
    return arr[Math.floor(Math.random() * arr.length)]
}

class Main extends React.Component {
    state = {
        listColor: [
            ['rgba(0,36,155,0.8)', 'rgba(26,0,87,0.8)'],
            ['rgba(155,0,0,0.8)', 'rgba(87,0,0,0.8)']
        ]
    }

    _renderItem = ({ item, section }) => <Item item={item} listColor={this.state.listColor} />
}

class Item extends Component {
    rando = _handleRandomIndex(this.props.listColor);

    render() {
        const { item } = this.props;

        return (
            <TouchableScale
                style={styles.row}
                onPress={this._onRowPress.bind(this, item)}
                activeScale={0.94}
                tension={0}
                friction={3}
            >
                <View style={styles.elevationLow} borderRadius={9}>
                    <ImageBackground source={{ uri: this._handleRandomIndex(item.bgImgs).image_link }} borderRadius={9} style={styles.imageBackground}>
                        <LinearGradient
                            colors={this.rando}
                            start={[0.1, 0.1]}
                            end={[0.5, 0.5]}
                            style={{ padding: 20, borderRadius: 9 }}>

                        </LinearGradient>
                    </ImageBackground>
                </View>
            </TouchableScale>
        )
    }
}

关于javascript - 颜色在 sectionList 上保持随机化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49702237/

相关文章:

javascript - 如何替换特定范围内的字符串?

javascript - 循环动态命名的数组

reactjs - Jest 的模拟常量不起作用(同一测试文件中的多个模拟)

ios - 执行 react-native run-ios 时出错

android - 如何将 elasticsearch 与 native react 一起使用?

javascript - 如果选中另一个复选框,则选中复选框

twitter-bootstrap - 如何在 React、bootstrap 中使插入符号变大?

reactjs - React.js,如何将多部分/表单数据发送到服务器

reactjs - 在 <Text/> 元素中设置单个单词的样式

javascript - IE9 IFrame 显示问题 - 控件在刷新之前不呈现