0

I have the following React-Native code which alerts the user that it was pressed, however it does not respond to anything.

I have tried using different Touchable components but none seem to work, the only one is Button but that's not really what I want, I'm building a slider so a button is not really appropriate for this.

Anyway, here's my code:

import React, {Component} from 'react';
import {View, TouchableWithoutFeedback, Alert, Text} from 'react-native';
import styles from './styles.js';
import LinearGradient from 'react-native-linear-gradient';

export default class BrightnessSlider extends Component {
    value: 100;

    constructor(...args) {
        super(...args);
    }

    render() {
        return (
            <LinearGradient start={{x: 0, y: 0}} end={{x: 1, y: 0}} colors={["#222", "#ddd"]} style={styles.brightnessSliderRunnable}>
                <View style={styles.brightnessSliderTrack}>
                    <TouchableWithoutFeedback onPress={() => Alert.alert("Hello World")}>
                        <View style={styles.brightnessSliderThumb} />
                    </TouchableWithoutFeedback>
                </View>
            </LinearGradient>
        )
    }
}

and ./styles.js

import {StyleSheet} from "react-native";

export default styles = StyleSheet.create({
    brightnessSliderRunnable: {
        top: 50,
        height: 9,
        width: width - 20,
        left: 10,
        backgroundColor: "#555",
        opacity: 1,
        elevation: 1,
        borderWidth: 0
        // position: "absolute"

    },
    brightnessSliderThumb: {
        width: 23,
        height: 23,
        borderColor: "#888",
        // opacity: 1,
        borderWidth: 2,
        backgroundColor: "#ffffff",
        position: "absolute",
        top: -7,
        borderRadius: 5,
        elevation: 2
    },
    brightnessSliderTrack: {
        width: "100%",
        elevation: 1,
        borderWidth: 0
    }
});

Thanks for any help

J-Cake
  • 1,526
  • 1
  • 15
  • 35

1 Answers1

0

Questioner's original code.

Solution

Added by questioner's solution.


Change position: absolute to position: relative.


Change your style type made by StyleSheet as below.

Your original.

{...styles.colourContainer, opacity: 100 - this.darkness}

Recommend way.

[styles.colourContainer, {opacity: 100 - this.darkness}]

I tested code and it works well when we touch the white square.

Tested on iOS.

enter image description here

Original App.js.

App.js changed by me.

import React, {Component} from 'react';
import {View, Dimensions, Text} from 'react-native';

import BrightnessSlider from '../components/BrightnessSlider';
import styles from '../components/TempStyle.js';

const d = Dimensions.get('window'),
  width = d.width,
  height = d.height;

export default class BrightnessSliderTest extends Component {
  constructor(...args) {
    super(...args);

    this.darkness = 0;

    this.prevColours = ["#ff0000", "#00ff00", "#0000ff"];
  }

  render() {

    // It is not possible just access the style created by StyleSheet.create(). Do not use StyleSheet.create() or change to flatten the StyleSheet object.
    const sty = [styles.colourContainer,{ opacity: 100 - this.darkness}];

    return (
      <View style={styles.container}>
        <View style={sty}>
          <View style={styles.toolBar}>
            <View style={styles.colourPalette}>
              {this.prevColours.map((i, a) => <View key={String(a)} style={[styles.colourTile, {backgroundColor: i}]}/>)}
            </View>
            {/* <Button title={"Save To Palette"} onTap={this.saveToPalette}>Save</Button> */}
          </View>
          <BrightnessSlider />
        </View>
      </View>
    );
  }
}

Why?

I am not sure how you can use your component in other components. But if you do not have container having enough size can show your component, touch area may not work. That is why I use backgroundColor for container sometimes in testing.


Added after testing full code used in question.

It is not possible just access the style through like spread operator created by StyleSheet.create(). Do not use StyleSheet.create() or change to flatten the StyleSheet object.

And Style can be worked differently from platforms(iOS, Android).

Jeff Gu Kang
  • 4,749
  • 2
  • 36
  • 44
  • I've tried this in 3 different react-native projects, same result there, "try again" isn't going to help – J-Cake Jan 13 '19 at 07:01
  • Could it have something to do with the platform? – J-Cake Jan 13 '19 at 07:03
  • @JacobSchneider Or try convert `LinearGradient` to normal `View`. – Jeff Gu Kang Jan 13 '19 at 07:07
  • I updated answer. Try using my code with style. And it will only work when you touch the white square you know. I am not sure how you can use your component in others. But if you do not have container having enough size can show your component, touch area may not work. That is why I use `backgroundColor` for container sometimes in testing. I hope you find the solution. – Jeff Gu Kang Jan 13 '19 at 07:08
  • Hey man thanks so much for your answer, but unfortunately, nothing worked, I've been stuck on this for more than DAYS – J-Cake Jan 13 '19 at 07:16
  • @JacobSchneider It will be better to show your other component's code to use this or full codes. The problem may have occurred elsewhere. – Jeff Gu Kang Jan 13 '19 at 07:18
  • Sure, [here's a Gist](https://gist.github.com/J-Cake/4a9b67fbf4a2b275c683bae24a3d0feb) – J-Cake Jan 13 '19 at 07:22
  • @JacobSchneider Updated answer. Focus on your style way. – Jeff Gu Kang Jan 13 '19 at 08:03
  • Hey man, could you update your answer to say "change `position: "absolute"` to `position: "relative"`? Cause that did it for me. – J-Cake Jan 14 '19 at 04:00
  • @JacobSchneider May you ran your code on android only. – Jeff Gu Kang Jan 14 '19 at 06:05
  • Some style works diferrently according to platforms. Anyway, congratulations to solve the problem. – Jeff Gu Kang Jan 14 '19 at 06:52
  • @JacobSchneider Added your solution for others. Happy coding! – Jeff Gu Kang Jan 14 '19 at 10:07
  • Thanks so much for your time. I was able to get it working and hence can progress with my app. – J-Cake Jan 14 '19 at 10:20