• react-native中的setNativeProps


    如果你通过React.createClass方法自定义了一个组件,直接给它设置样式 prop 是不会生效的,你得把样式 props 层层向下传递给子组件
    ,直到子组件是一个能够直接定义样式的原生组件。同理,我们也需要把setNativeProps传递给由原生组件封装的子组件。
    具体要做的就是在我们的自定义组件中再封装一个setNativeProps方法,其内容为对合适的子组件调用真正的setNativeProps方法,并传递要设置的参数。

    import React from 'react';
    import { Text, TouchableOpacity, View } from 'react-native';
    
    class MyButton extends React.Component {
      setNativeProps = (nativeProps) => {
        this._root.setNativeProps(nativeProps);
      }
    
      render() {
        return (
          <View ref={component => this._root = component} {...this.props}>
            <Text>{this.props.label}</Text>
          </View>
        )
      }
    }
    
    export default class App extends React.Component {
      render() {
        return (
          <TouchableOpacity>
            <MyButton label="Press me!" />
          </TouchableOpacity>
        )
      }
    }
    

    setNativeProps to clear TextInput value
    先看效果图

    import React from 'react';
    import { TextInput, Text, TouchableOpacity, View } from 'react-native';
    
    export default class App extends React.Component {
      clearText = () => {
        this._textInput.setNativeProps({text: ''});
      }
    
      render() {
        return (
          <View style={{flex: 1}}>
            <TextInput
              ref={component => this._textInput = component}
              style={{height: 50, flex: 1, marginHorizontal: 20, borderWidth: 1, borderColor: '#ccc'}}
            />
            <TouchableOpacity onPress={this.clearText}>
              <Text>Clear text</Text>
            </TouchableOpacity>
          </View>
        );
      }
    }
    

    demo自:https://reactnative.cn/docs/direct-manipulation/

  • 相关阅读:
    mysql索引最左匹配的理解(转载于知乎回答)
    mysql深度优化与理解(迄今为止读到最优秀的mysql博客)
    PHP数组函数总结与使用
    进程(process)和线程(thread)
    联合索引使用规则(转载)
    mysql优化大全(转自别人 )
    HTTP隧道解决的问题
    HTTP代理协议 HTTP/1.1的CONNECT方法
    vant弹窗提示
    vue获取验证码倒计时
  • 原文地址:https://www.cnblogs.com/smart-girl/p/10648639.html
Copyright © 2020-2023  润新知