Satoru Kikuchi
Satoru Kikuchi

Reputation: 1209

How to make text message clickable with react-native-gifted-chat

I'm developing a mobile chat app with react-native-gifted-chat, and I would like to make the system messages clickable to execute a function.

My code is below, but somehow it doesn't work.

import React, { Component } from 'react';
import { Image, ImageBackground, Text, Linking, SafeAreaView, ScrollView, StyleSheet, View } from 'react-native';
import { Body, Container, Header, Icon, Left, Right, Thumbnail } from "native-base";
import { Button, List } from 'react-native-paper';
import { GiftedChat, Composer } from 'react-native-gifted-chat';

export default class ChatScreen extends Component {

  messages = [
    {
      _id: 1,
      text: <Text onClick={() => { alert('hello')}} style={{ color: 'red' }}>This message can not be clickable</Text>,
      createdAt: new Date(Date.UTC(2016, 5, 11, 17, 20, 0)),
      system: true,
    }
  ];

  renderComposer = props => {
    return (
      <View style={{flexDirection: 'row'}}>
        <Icon type='SimpleLineIcons' name='paper-clip' style={{ fontSize: 20, justifyContent: 'center', paddingTop: 10, paddingLeft: 5 }}/>
        <Composer {...props} />
        <Button>Submit</Button>
      </View>
    );
  };

  render() {
    return (
      <Container>
        <GiftedChat
          renderComposer={this.renderComposer}
          messages={this.messages}
        />
      </Container>
    )
  }
}

});

And this is the screenshot of my simulator. https://gyazo.com/bc1facffffcbe868fbce5cb15385890d

I expect the system message 'This message cannot be clickable' should be clickable and it shows up the alert message 'hello' when clicking it. But it doesn't work.

Upvotes: 1

Views: 5911

Answers (2)

ExpertsfromIndia
ExpertsfromIndia

Reputation: 11

The Text has an onPress props not onClick, So you just need to change onClick to onPress. If you will change this then your message will look like this

messages = [
    {
      _id: 1,
      text: <Text onPress={() => { alert('hello')}} style={{ color: 'red' }}>This message can not be clickable</Text>,
      createdAt: new Date(Date.UTC(2016, 5, 11, 17, 20, 0)),
      system: true,
    }
  ];

This will definitely work for you!

Upvotes: 1

SungHo Choi
SungHo Choi

Reputation: 103

Would you try with onPress instead of onClick in Text component?

And you can also refer this below.

https://facebook.github.io/react-native/docs/text#onpress

Upvotes: 1

Related Questions