Pass ref as function parameter React Native


Brinto

In the current code, I use ref to change TextInputthe style of a component that uses a function in a method setNativeProps.

class RegisterScreen extends Component {
    constructor(props) {
        super (props)
        this.state = {
            borderBottomStyle: '#f7f7f7'
        }
    }

    focusedInput = () => { 
        this.textInput.setNativeProps({
            style: { borderBottomColor: '#445AE3' }
        }) 
    }
    
    blurredInput = () => { 
        this.textInput.setNativeProps({
            style: { borderBottomColor: '#f7f7f7' }
        }) 
    }

    render() {
        return (
            <View style={styles.container} >
               <View style={styles.form}>
                    <TextInput
                        ref={c => { this.textInput = c}} 
                        style={styles.textInput}
                        onFocus={this.focusedInput}
                        onBlur={this.blurredInput}
                        style={[styles.formInput, { borderBottomColor: this.state.borderBottomStyle }]}
                        placeholder={"Email"}
                    />
                    <TextInput
                        style={[styles.formInput, { borderBottomColor: this.state.borderBottomStyle }]}
                        placeholder={"Password"}
                    />
               </View>
            </View>
        )
    }
}

export default RegisterScreen

It's compatible with my first one, TextInputbut I'm looking for a way to do the same with the second one. I first thought of passing a ref to the focusedInputand blurredInputfunction as a parameter to specify the element to modify, but I'm not very familiar with refs.

Is there a way to achieve this?

thank you for your help

new developer

If I understand your question correctly, you want to conditionally change the UI of the textbox based on whether or not the focus is on.

You can use just one state to keep the currentFocused textInput name and conditionally change the UI of the textbox based on the currentFocused box.

I did a quick demo on sandbox. Please see the link below.

Demo Sandbox Link

import React, { Component } from "react";
import { View, TextInput } from "react-native";

class RegisterScreen extends Component {
  constructor(props) {
    super(props);
    this.state = {
      borderBottomStyle: "#f7f7f7",
      currentlyFocused: null
    };
  }

  focusedInput = () => {
    this.textInput.setNativeProps({
      style: { borderBottomColor: "#445AE3" }
    });
  };

  blurredInput = () => {
    this.textInput.setNativeProps({
      style: { borderBottomColor: "#f7f7f7" }
    });
  };

  render() {
    return (
      <View style={{ flex: 1 }}>
        <View>
          <TextInput
            style={{
              borderBottomColor:
                this.state.currentlyFocused === "email" ? "red" : "grey",
              borderStyle: "solid",
              borderWidth: this.state.currentlyFocused === "email" ? 3 : 0,
              height: 50,
              marginTop: 20
            }}
            onFocus={() => this.setState({ currentlyFocused: "email" })}
            onBlur={() => this.setState({ currentlyFocused: "" })}
            placeholder={"Email"}
          />
          <TextInput
            style={{
              borderBottomColor:
                this.state.currentlyFocused === "password" ? "red" : "grey",
              borderStyle: "solid",
              borderWidth: this.state.currentlyFocused === "password" ? 3 : 0,
              height: 50,
              marginTop: 20
            }}
            onFocus={() => this.setState({ currentlyFocused: "password" })}
            onBlur={() => this.setState({ currentlyFocused: "" })}
            placeholder={"Password"}
          />
        </View>
      </View>
    );
  }
}

export default RegisterScreen;

Related


Pass ref as function parameter React Native

Brinto In the current code, I use ref to change TextInputthe style of a component that uses a function in a method setNativeProps. class RegisterScreen extends Component { constructor(props) { super (props) this.state = { border

Pass ref as function parameter React Native

Brinto In the current code, I use ref to change TextInputthe style of a component that uses a function in a method setNativeProps. class RegisterScreen extends Component { constructor(props) { super (props) this.state = { border

Pass ref as function parameter React Native

Brinto In the current code, I use ref to change TextInputthe style of a component that uses a function in a method setNativeProps. class RegisterScreen extends Component { constructor(props) { super (props) this.state = { border

Pass ref as function parameter React Native

Brinto In the current code, I use ref to change TextInputthe style of a component that uses a function in a method setNativeProps. class RegisterScreen extends Component { constructor(props) { super (props) this.state = { border

Pass ref as function parameter React Native

Brinto In the current code, I use ref to change TextInputthe style of a component that uses a function in a method setNativeProps. class RegisterScreen extends Component { constructor(props) { super (props) this.state = { border

How to pass parameter to function without binding in React-Native

Arester So, I've read doing something like this <SomeButton onPress={() => { this.someFunction(args)}} /> This is bad because it creates a new instance of the function for each render cycle. But how do I pass args in React-Native? Tolle Creating a new inline f

How to pass parameter to function without binding in React-Native

Arester So, I've read doing something like this <SomeButton onPress={() => { this.someFunction(args)}} /> This is bad because it creates a new instance of the function for each render cycle. But how do I pass args in React-Native? Tolle Creating a new inline f

How to pass parameter to function without binding in React-Native

Arester So, I've read doing something like this <SomeButton onPress={() => { this.someFunction(args)}} /> This is bad because it creates a new instance of the function for each render cycle. But how do I pass args in React-Native? Tolle Creating a new inline f

Pass parameter to component in React Native

Nimila Hiranya : I'm trying to use a generic navigation component I made in React-Native. While on a call, I want to set the navbar's tint, title, etc. Navigation barcodes: var NavigationBar = React.createClass({ render: function(title, titleColor, NavBarC

Pass parameter to component in React Native

Nimila Hiranya : I'm trying to use a generic navigation component I made in React-Native. While on a call, I want to set the navbar's tint, title, etc. Navigation barcodes: var NavigationBar = React.createClass({ render: function(title, titleColor, NavBarC

Pass parameter to component in React Native

Nimila Hiranya I'm trying to use a generic navigation component I made in React-Native. While on a call, I want to set the navbar's tint, title, etc. Navigation barcodes: var NavigationBar = React.createClass({ render: function(title, titleColor, NavBarCol

react native pass parameter to navigator

Jeroke I have a tab navigator and I would like to know if it is possible to pass a parameter to the tab navigator and use that parameter in the HomePage component. I am using to pass parameters from Drawer Navigator this.props.navigation.navigate('TabsNav', {t

Pass parameter to component in React Native

Nimila Hiranya : I'm trying to use a generic navigation component I made in React-Native. While on a call, I want to set the navbar's tint, title, etc. Navigation barcodes: var NavigationBar = React.createClass({ render: function(title, titleColor, NavBarC

react native pass parameter to navigator

Jeroke I have a tab navigator and I would like to know if it is possible to pass a parameter to the tab navigator and use that parameter in the HomePage component. I am using to pass parameters from Drawer Navigator this.props.navigation.navigate('TabsNav', {t

React: Pass parameter to function in map

Yuki I'm building a component that uses MaterialDesign's table to display an array of users in React. Each row is generated by a mapping function: let actions = this.props.rowButtons; ... {this.props.tableData.map( (val, index) => ( <Tab

react pass component as parameter to function

Kustermit I have a function that takes a Component as its parameter. This feature allows the user to present their own popup instead of the one I provided. However, I can't add some props to the component until I add it to the array. const addCustomSnack = (Sn

react pass component as parameter to function

Kustermit I have a function that takes a Component as its parameter. This feature allows the user to present their own popup instead of the one I provided. However, I can't add some props to the component until I add it to the array. const addCustomSnack = (Sn

React Native: Pass string as function

Chris Henry Actions allow me to load the page from the button. So I would have eg "page1" as the key (it's a function) which I can apply to Actions.page1 and navigate to that page. However, here I only get the string of this.props.lec which contains the same c

Native pass parameter in react route becomes undefined

Beto IGM I am trying to send parameters from screen A to screen B like this: Here's what I'm trying so far: await Auth.signIn(email, password) .then((user) => { navigation.navigate('SingInConfirm', {user: user}); }) On screen B, I want to rec

Native pass parameter in react route becomes undefined

Beto IGM I am trying to send parameters from screen A to screen B like this: Here's what I'm trying so far: await Auth.signIn(email, password) .then((user) => { navigation.navigate('SingInConfirm', {user: user}); }) On screen B, I want to rec

Pass parameter to custom header React Native

Aimen Aimen I have a screen that shows a flat list of some doctors I get from the server. Clicking on one of them now leads you to his profile screen with a custom title. On this header I want to show his picture and name: But the title component is on my stac

Native pass parameter in react route becomes undefined

Beto IGM I am trying to send parameters from screen A to screen B like this: Here's what I'm trying so far: await Auth.signIn(email, password) .then((user) => { navigation.navigate('SingInConfirm', {user: user}); }) On screen B, I want to rec