Focus style for TextInput in react-native

后端 未结 7 1477
小鲜肉
小鲜肉 2020-12-05 17:56

In React Native, how do you change the style of a textInput when it gets focus? Say I have something like

class MyInput extends Component {
             


        
7条回答
  •  盖世英雄少女心
    2020-12-05 18:33

    Use refs, DirectManipulation and setNativeProps for more performance: https://facebook.github.io/react-native/docs/direct-manipulation.

    class MyInput extends Component {
      focusedInput = () => { 
        this.textInput.setNativeProps({
          style: { backgroundColor: 'green' }
        }) 
      }
    
      blurredInput = () => { 
        this.textInput.setNativeProps({
          style: { backgroundColor: 'yellow' }
        }) 
      }
    
      render () {
          return  { this.textInput = c}} 
                    style={styles.textInput}
                    onFocus={this.focusedInput}
                    onBlur={this.blurredInput} />
      }
    

    }

    const stylesObj = { textInput: { height: 50, fontSize: 15, backgroundColor: 'yellow', color: 'black', } }

    const styles = StyleSheet.create(stylesObj)

    This updates the TextInput component directly without re-rendering the component hierarchy.

提交回复
热议问题