React Native中的动态色彩

瓦西夫·科曼尼

我是新来的本地人。我想根据选择为应用程序创建动态颜色。

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */
import React, {
    AppRegistry,
    Component,
    StyleSheet,
    Text,
    Image,
    NavigatorIOS,
    AlertIOS,
    ListView,
    ScrollView,
    TouchableHighlight,
    View
} from 'react-native';
import {
    times,
    uniqueId
} from 'lodash';

var Accordion = require('react-native-accordion/src/index.js');
var my = '#eee';
class AwesomeProject extends Component {

    render() {

        return ( 
            <View style = {styles.group} >
                <NavigatorIOS style = {styles.group}
                initialRoute = {{
                        component: AccordionList,
                        title: 'Color Selector',
                        rightButtonIcon: require('./img/a.png'),
                        onRightButtonPress: () => {

                            AlertIOS.alert(
                                'Select Color',
                                null, [{
                                    text: 'Red',
                                    onPress: () =>setColor( 'red'),
                                }, {
                                    text: 'Blue',
                                    onPress: () => setColor( 'blue'),
                                }]
                            );

                        }
                    }
                }
                barTintColor = "#0391D7"
                titleTextColor = "#fff"
                tintColor = '#fff' />
                <View style = {styles.line}/> 
            </View>
        );
    }
}
setColor = function(color){
    if(color == 'blue')
        my = '#eee';
    else 
        my = '#aaa';
    }

const AccordionList = React.createClass({
    getInitialState() {
        const ds = new ListView.DataSource({
            rowHasChanged: (r1, r2) => r1 !== r2
        });
        return {
            dataSource: ds.cloneWithRows(times(6, uniqueId.bind(null, 'ball_'))),
        };
    },

    render() {

        return (
            <View style = {{flex: 1}} >
                <ListView dataSource = {this.state.dataSource}
                renderRow = {this._renderRow}
                /> 
            </View>
        );
    },

    _renderHeader() {
        return ( <View style = { styles.listView } >
                <Image style = {styles.icon}
                source = {require('./img/1.jpg')}/> 
                <Text> Click to Expand {my}< /Text> 
            </View>
        );
    },

    _renderContent() {
        return ( <View style = {styles.container} >
            <Text style = {styles.welcome} >
            {
                'greeting'
            }
            Welcome to React Native!
            </Text> 
            <Text style = {
                styles.instructions
            } >
            To get started, edit index.ios.js 
            </Text> 
            <Text style = {
                styles.instructions
            } >
            Press Cmd + R to reload, {
                '\n'
            }
            Cmd + D or shake
            for dev menu
            </Text> 
        </View>
        );
    },

    _renderRow(rowData) {

        return ( <Accordion header = {
                this._renderHeader()
            }
            content = {
                this._renderContent()
            }
            duration = {
                300
            }
            easing = "easeOutCubic" />
        );
    }
});

const styles = StyleSheet.create({
    icon: {
        height: 20,
        width: 20,
        alignItems: 'flex-end',
        borderWidth: 1
    },
    listView: {
        alignItems: 'flex-end',
        paddingTop: 15,
        paddingRight: 15,
        paddingLeft: 15,
        paddingBottom: 15,
        borderBottomWidth: 1,
        borderBottomColor: my,
        backgroundColor: my,
    },
    line: {
        backgroundColor: '#bbbbbb',
        height: StyleSheet.hairlineWidth,
    },
    container: {
        flex: 1,
        borderWidth: 1,
        justifyContent: 'center',
        alignItems: 'flex-end',
        backgroundColor: '#F5FCFF',
    },
    welcome: {
        fontSize: 20,
        textAlign: 'center',
        margin: 10,
    },
    instructions: {
        textAlign: 'center',
        color: '#3333f3',
        marginBottom: 5,
    },
    group: {
        backgroundColor: 'white',
        flex: 1
    },
    groupSpace: {
        height: 15,
        padding: 10
    },
});
const styles_a = StyleSheet.create({
    icon: {
        height: 30,
        width: 30
    },
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#F5FCFF',
    },
    line: {
        backgroundColor: '#bbbbbb',
        height: StyleSheet.hairlineWidth,
    },
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#F5FCFF',
    },
    welcome: {
        fontSize: 20,
        textAlign: 'center',
        margin: 10,
    },
    instructions: {
        textAlign: 'center',
        color: '#3333f3',
        marginBottom: 5,
    },

    listView: {
        alignItems: 'flex-start',
        paddingTop: 15,
        paddingRight: 15,
        paddingLeft: 15,
        paddingBottom: 15,
        borderBottomWidth: 1,
        borderBottomColor: my,
        backgroundColor: '#fafafa',
    },
    group: {
        backgroundColor: 'white',
        flex: 1,
        padding: 10
    },
    groupSpace: {
        height: 15,
        padding: 10
    },
});

AppRegistry.registerComponent('AwesomeProject', () => AwesomeProject);

每当我选择一种新颜色并发出警报时,它就会向我显示颜色选择,但不会更新列表的背景颜色。

不知道我在做什么错。

尼科

试试这个:

不要将颜色存储在全局变量中,而应将其存储在组件的状态中。如果您不知道状态在React中意味着什么,请考虑阅读本文

我看到您调用了setColor函数来更改变量my相反,您可以致电

this.setState({currentColor: 'red'})

然后在一个render()方法中,可以将其附加到数组以覆盖默认样式:

<SomeComponent style={[styles.myStyle, {backgroundColor: this.state.currentColor}]}/>

这样做的目的是setState触发新的UI呈现。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在React Native中动态创建组件

来自分类Dev

在React Native中动态指定JSON

来自分类Dev

React Native 动态样式

来自分类Dev

从React Native中的数组映射功能动态渲染内容

来自分类Dev

在React Native中动态更改图像URL

来自分类Dev

如何在React native中动态设置图像的来源?

来自分类Dev

无法在React Native中动态设置标题标题

来自分类Dev

在React Native App中从WebView打开动态链接

来自分类Dev

根据react-native中的props值动态加载图像?

来自分类Dev

如何在 React Native 中通过点击设置动态样式?

来自分类Dev

在 react-native 中动态更改 View 的背景颜色

来自分类Dev

在 react-native 中动态隐藏导航标题

来自分类Dev

如何从 React Native 中的包动态获取数据

来自分类Dev

在芯片的Angular动态色彩中

来自分类Dev

React Native动态Webview高度

来自分类Dev

创建动态组件 React (Native)

来自分类Dev

React Native 动态更改 drawerLabel

来自分类Dev

React 中的动态需求

来自分类Dev

如何在React Native EXPO中的Dropdown选项中动态获取api数据

来自分类Dev

在 React Native 中,如何从 json 数组动态获取平面列表中的键

来自分类Dev

React Native中的zIndex

来自分类Dev

React Native中的海拔

来自分类Dev

在React Native中链接

来自分类Dev

在React Native(iOS)中支持动态类型

来自分类Dev

传递动态道具React-native

来自分类Dev

React-native,动态渲染按钮单击

来自分类Dev

动态渲染MapView标记React Native

来自分类Dev

React native-动态更改样式

来自分类Dev

React Native:动态样式类变量