Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native - What is reactTag parameter in AccessibilityInfo.setAccessibilityFocus()?

What is reactTag parameter in AccessibilityInfo.setAccessibilityFocus(reactTag) method? React native documentation don't provide any information about this parameter:

Set accessibility focus to a React component. On Android, this is equivalent to UIManager.sendAccessibilityEvent(reactTag, UIManager.AccessibilityEventTypes.typeViewFocused);.

I don't have any background of Objective-C and Java. A little example will be more appreciated. Thank !!!

like image 799
Stack Overflow Avatar asked Nov 25 '18 07:11

Stack Overflow


People also ask

What is reactTag?

reactTag is simply a number that is used by react to identify view objects in your application. It is the result of findNodeHandle function, which takes a view reference as parameter.

What is onMagicTap?

onMagicTap In the Phone app on iPhone, a magic tap answers a phone call, or ends the current one. If the selected element does not have an onMagicTap function, the system will traverse up the view hierarchy until it finds a view that does.

How do you check accessibility in react native?

On Android, you can use Accessibility Scanner. To use it with an emulator, download the Accessibility Inspector APKand then drag-and-drop to your emulator.


1 Answers

reactTag is simply a number that is used by react to identify view objects in your application. It is the result of findNodeHandle function, which takes a view reference as parameter.

Here's a simple example on how you can use it:

import React, {Component} from 'react'
import {
  ...
  findNodeHandle,
  ...
} from 'react-native';

class Sample extends React.Component {
    constructor(props) {
        super(props)
        this.viewRef = null;
    }

    ...

    componentDidMount() {
        if (this.viewRef) {
            const reactTag = findNodeHandle(this.viewRef);
            AccessibilityInfo.setAccessibilityFocus(reactTag);
        }
    }

    render() {
        return (
            <View ref={el => { this.viewRef = el }}>
                ...
            </View>
        )
    }
}
like image 85
M Reza Avatar answered Oct 02 '22 16:10

M Reza