gluestack

gluestack Logogluestack

onPress not working on <Text> element

tread2779 posted this in #support
Open in Discord
Avatar
tread2779OP
Today is my first day with gluestack, so I'm just playing around, but I ran into an issue:

The gluestack <Text> component throws an error in my web browser when I include the onPress prop:
Warning: Unknown event handler property `onPress`. It will be ignored.

What's interesting is that onPress works fine with React Native's <Text> element (see below)
Wrapping gluestack's <Text> in <Pressable> works fine, too.

Is this is a bug?

import { Fragment } from 'react';
import { Heading } from './ui/heading';
import { Text } from './ui/text';
import { VStack } from './ui/vstack';
import { Text as ReactNativeText } from 'react-native';
import { Pressable } from './ui/pressable';

function ListGroup() {
  let items = [
    'New York',
    'Los Angeles',
    'Chicago',
  ];

  return (
    <>
      <VStack className="mx-2 rounded border border-b-0 border-gray-300">
        {items.length === 0 && <Text>No Items</Text>}
        {items.map((item) => (
          <Fragment key={crypto.randomUUID()}>
            <Text
              key={crypto.randomUUID()}
              onPress={() => console.log('pressed from text element')} // <<< This causes the error
              className="select-none border-b-2 py-2 pl-4 text-orange-500"
            >
              {item}
            </Text>
            <Pressable onPress={() => console.log('pressed from pressable')}>
              <Text
                key={crypto.randomUUID()}
                className="select-none border-b-2 py-2 pl-4 text-blue-500"
              >
                {item}
              </Text>
            </Pressable>
            <ReactNativeText
              onPress={() => console.log('pressedReactNative')}
              key={crypto.randomUUID()}
              className='select-none border-b-2 py-2 pl-4 text-green-500'
            >
              {item}
            </ReactNativeText>
          </Fragment>
        ))}
      </VStack>
    </>
  );
}

export default ListGroup;

6 Replies

Avatar
sravankumar101
hey @tread2779 I've checked the text component in expo application. The onpress is working as expected.
Could you please provide more details of the issue. what platform you are testing/ compiler you are using to up the application (like react native / metro/ expo).
Avatar
tread2779OP
I made a repo (branch) to show the issue git clone -b onpress-issue https://github.com/resticker/gluetest1.git

Just use yarn web and see the console error
Image
Avatar
sravankumar101
hey @tread2779 sorry my bad, Looking into this , please give us some time. We will revert back to you with a workaround for this.
Avatar
sravankumar101
Hey @tread2779
In web Text converts to span . As span doesn't support onPress to achieve similar type of behaviour on web you can
1. use text directly from react-native , but that won't be RSC compatible.
2.You can simply wrap your text component with Pressable.
Avatar
tread2779OP
Ok. thanks @sravankumar101 I really appreciate you looking into this 😁