代码之家  ›  专栏  ›  技术社区  ›  csb00

onChange函数不工作的条件

  •  0
  • csb00  · 技术社区  · 7 年前

    我是一个RN新手,刚刚完成了一个教程,在这个教程中,我可以创建一个组件,您可以在拖动圆圈的同时选择一个数字。但是,现在我希望在拖动时颜色随每个不同的值/数字而变化。现在,我把它写下来,当值等于数字2时,它变为红色。我最初的想法是在onChange函数中创建一个条件,但到目前为止还没有成功。它一点也不破,但不会变色。有人有什么建议吗?下面是代码和我制作的图片。

    import React, { Component } from "react";
    import { StyleSheet, View, Text } from "react-native";
    import CircleSizeSelector from "react-native-circle-size-selector";
    type State = {
      value: number
    };
    const InitialValue = 1;
    export default class CircleSize extends Component<void, State> {
        static navigationOptions = {
            title: "Question of the day",
            headerStyle: {
              backgroundColor: "#53b4e6"
            },
            headerTintColor: "#f6c945",
            headerTitleStyle: "bold"    
          };
      state: State = {
        value: InitialValue
      };
      onChange = (value: number) => {
        this.setState( value => {
          if (value === "number") {
            if (this.onChange.value === "2"){
            return { color: "red"};
            }
          }
        });
      };
      render() {
        return (
          <View style={styles.container}>
              <View>
                  <Text style={styles.question}>On a scale from 1 to 10, how do you rank Sunday's episode?</Text>
              </View>
            <View style={styles.parent}>
              <CircleSizeSelector
                minValue={1}
                maxValue={10}
                initialValue={InitialValue}
                onChange={this.onChange}
              >
                <View>
                  <Text style={styles.text}>{this.state.value}</Text>
                </View>
              </CircleSizeSelector>
            </View>
          </View>
        );
      }
    }
    

    Image of that I created. Would like to have the colors change as I drag and change the value

    0 回复  |  直到 7 年前
        1
  •  1
  •   Mehran Khan    7 年前

    为了在拖动时随每个不同的值/数字进行更改,我定义了一个颜色数组,并根据状态中设置的值选择颜色。

    import CircleSizeSelector from "react-native-circle-size-selector";
    import { StyleSheet, View, Text } from "react-native";
    import React from "react";
    
    const colors = [
      "#FF6633",
      "#FFB399",
      "#FF33FF",
      "#FFFF99",
      "#00B3E6",
      "#E6B333",
      "#3366E6",
      "#999966",
      "#99FF99",
      "#B34D4D"
    ];
    
    export default class App extends React.Component {
      state = {
        value: 1
      };
    
      onChange = value => {
        this.setState({ value });
      };
    
      render() {
        const { value } = this.state;
        const backgroundColor = colors[value - 1];
        return (
          <View style={styles.container}>
            <CircleSizeSelector
              minValue={1}
              maxValue={10}
              initialValue={this.state.value}
              onChange={this.onChange}
              currentValueCircleStyle={{
                backgroundColor: backgroundColor
              }}
              resizingCurrentValueCircleStyle={{
                backgroundColor: backgroundColor
              }}
            >
              <Text>{this.state.value}</Text>
            </CircleSizeSelector>
          </View>
        );
      }
    }
    
    const styles = StyleSheet.create({
      container: {
        flex: 1,
        justifyContent: "center",
        alignItems: "center",
        backgroundColor: "#F5FCFF"
      }
    });
    

    See The App Preview

        2
  •  1
  •   azundo    7 年前

    条件函数将对您调用的方式进行一些调整 this.setState [1]. 的更新程序版本 setState onChange 处理程序。但是根据你所描述的,我认为你应该能够使用更简单的 stateChange 版本 它只返回一个要合并到组件状态的对象。你也可以打电话 设定状态 将对多次更新进行批处理。

    onChange = (value: number) => {
        // update state.value to the value from the SizeSelector
        this.setState({value});
        // conditionally change the color
        // this setState will get batched with the above if it executes
        if (value === 2) {
           this.setState({color: 'red'});
        }
      };
    

    [1] https://reactjs.org/docs/react-component.html#setstate

        3
  •  1
  •   Dzmitry    7 年前

    state: State = {
      value: InitialValue,
      color: 'blue'
    };
    

    然后改变 onChange 功能取决于条件:

    onChange = (value: number) => {
      this.setState({ value })
      if (value===2) {
        this.setState({ color: 'red' })
      }
    

    CircleSizeSelector 组件取决于 state.color . 我不知道它会是什么样子,但类似这样:

    currentValueCircleStyle = { { backgroundColor: this.state.color } }