【问题标题】:dont share state between reusable component react native不要在可重用组件之间共享状态反应原生
【发布时间】:2020-06-21 07:39:25
【问题描述】:

我正在创建一个带有 onFocus 和 onBlur 动画的可重用 Text 组件,但是当我把它放在一个表单中时;焦点和模糊事件触发表单中每个输入的动画...你能帮我避免这种行为吗?

如果您需要更多详细信息,这里是代码,但我认为这很清楚

import React, { Component } from 'react';
import { TextInput, View, Text, Animated, StyleSheet } from 'react-native';

const animatedPlaceholder = new Animated.Value(30);

class Input extends Component {

constructor(props) {
 super(props);
 this.state = {
  id: '',
  isFocused: false,
  textLength: 0
 };
}

secureTextEntry = this.props.secureTextEntry || false;
autoCapitalize = this.props.autoCapitalize || 'sentences';
keyboardType = this.props.keyboardType || 'default';
focus = () => {
 this.setState({isFocused: true});
 Animated.timing(animatedPlaceholder, {
  toValue: 0,
  duration: 300
 }).start();
}

blur = () => {
 this.setState({isFocused: false});

  Animated.timing(animatedPlaceholder, {
    toValue: 30,
    duration: 300
  }).start();

}

render() {
 return(
  <View {...this.props}>
    <Animated.Text style={
      this.state.isFocused ? styles.usedValue : styles.emptyValue
    } > {this.props.placeholder} </Animated.Text>
    <TextInput
      onFocus={this.focus}
      onBlur={this.blur}
      autoCapitalize={this.autoCapitalize}
      secureTextEntry={this.secureTextEntry}
      keyboardType={this.keyboardType}
        style={
          styles.textInput
        }
      />
  </View>
);
}
}
export default Input;

【问题讨论】:

  • 你想要什么动画,能指定动画吗

标签: react-native react-redux


【解决方案1】:

我不太明白你的问题,但是我创建了一个组件,它在占位符聚焦时为它设置动画,如果值为空,则动画返回,

查看这个零食示例https://snack.expo.io/@ashwith00/frowning-cookie

代码

import React, { Component } from 'react';
import { TextInput, View, Text, Animated, StyleSheet } from 'react-native';

export default class Input extends Component {
  constructor(props) {
    super(props);
    this.state = {
      id: '',
      isFocused: false,
      textLength: 0,
    };
    this.animatedPlaceholder = new Animated.Value(0);
  }

  secureTextEntry = this.props.secureTextEntry || false;
  autoCapitalize = this.props.autoCapitalize || 'sentences';
  keyboardType = this.props.keyboardType || 'default';

  focus = () => {
    Animated.timing(this.animatedPlaceholder, {
      toValue: -40,
      duration: 300,
    }).start();
  };

  blur = () => {
    if (!this.props.value) {
    Animated.timing(this.animatedPlaceholder, {
      toValue: 0,
      duration: 300,
    }).start();
    }
  };

  render() {
    const {value, onChangeText} = this.props;
    return (
      <View  style={[ {
        justifyContent: 'center'
      }]}>
        <Animated.Text
          style={{
            position: 'absolute',
            transform: [{translateY: this.animatedPlaceholder}]
          }}>
          {' '}
          {this.props.placeholder}{' '}
        </Animated.Text>
        <TextInput
        value={value}
        onChangeText={onChangeText}
          onFocus={this.focus}
          onBlur={this.blur}
          autoCapitalize={this.autoCapitalize}
          secureTextEntry={this.secureTextEntry}
          keyboardType={this.keyboardType}
          style={styles.textInput}
        />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  usedValue: {} ,
  emptyValue: {},
  textInput: {
    alignSelf: 'stretch',
    height: 50,
    borderWidth: 0.4
  }
})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-31
    • 1970-01-01
    • 2019-02-18
    • 2016-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多