Home > OS >  ReactNative extended button props undefined
ReactNative extended button props undefined

Time:01-14

I try extend a Button component with a signature attribute to store specific data and add Button specific parameter to fetch. Why event.currentTarget.props is undefined?

import React from 'react';
import {
  Alert,
  Button,
  ButtonProps,
  GestureResponderEvent,
  StyleSheet,
  View,
} from 'react-native';

interface MyButtonProps extends ButtonProps {
  signature: string;
}
class MyButton extends React.Component<MyButtonProps, {}> {
  render() {
    return <Button {...this.props} />;
  }
}

const App = () => {
  const _onPressButton = (event: GestureResponderEvent) => {
    Alert.alert(JSON.stringify(event.currentTarget.props));
    //let params: string = (event.target as MyButton).props.signature;
    //fetch(`http://12.18.1.11/switch?${params}`);
  };

  return (
    <View style={styles.container}>
      <View style={styles.buttonContainer}>
        <MyButton
          signature="binary=000011110000011&protocol=1&pulselength=133"
          onPress={_onPressButton}
          title="LED1 (ON)"
        />
      </View>
      <View style={styles.buttonContainer}>
        <MyButton
          signature="binary=000111110&protocol=1&pulselength=133"
          onPress={_onPressButton}
          title="LED1 (OFF)"
        />
      </View>
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
  },
  buttonContainer: {
    margin: 20,
  },
});

export default App;

Maybe I not understand well the extension mechanism? Please help me.

CodePudding user response:

class MyButton extends React.Component<MyButtonProps, {}> {
  render() {
    return <Button title={this.props.signature} />;
  }
}

signature in title

CodePudding user response:

Check pressevent props it doesn't have props property.

List of pressevent properties:

{
    changedTouches: [PressEvent],
    identifier: 1,
    locationX: 8,
    locationY: 4.5,
    pageX: 24,
    pageY: 49.5,
    target: 1127,
    timestamp: 85131876.58868201,
    touches: []
}

If you need to access to component reference you can use direct manipulations

const viewRef = useRef<View>();
const setOpacityTo = useCallback(value => {
  // Redacted: animation related code
  viewRef.current.setNativeProps({
    opacity: value,
  });
}, []);

return (
  <TouchableOpacity
    onPressIn={() => setButtonOpacity(0.5)}
    onPressOut={() => setButtonOpacity(1)}>
  </TouchableOpacity>
);

Props you can access with target

<Button
 style={{margin: 30}}
 title="Change colors"
 onPress={(event) => {
   console.log(Object.keys(event))
   console.log(Object.keys(event.currentTarget))
   console.log(Object.keys(event.target))
   console.log(Object.keys(event.target.viewConfig))
   console.log(Object.keys(event.nativeEvent))
 }}
/>

results

iPhone:
►["dispatchConfig","_targetInst","nativeEvent","_dispatchListeners","_dispatchInstances","type","target","currentTarget","eventPhase","bubbles","cancelable","timeStamp","defaultPrevented","isTrusted","touchHistory","isDefaultPrevented","isPropagationStopped"]
iPhone:
►["_nativeTag","_children","viewConfig"]
iPhone:
►["target","pageY","force","locationY","locationX","identifier","pageX","timestamp","changedTouches","touches"]
iPhone:
►["validAttributes","directEventTypes","uiViewClassName"]
iPhone:
►["_nativeTag","_children","viewConfig"]

CodePudding user response:

I reorganized the code with a method call:

class MyButton extends React.Component<MyButtonProps, {}> {
  _onPressButton = (params: string) => {
    fetch(`http://10.0.1.119/switch?${params}`);
  };

  render() {
    return (
      <Button
        onPress={() => this._onPressButton(this.props.signature)}
        {...this.props}
      />
    );
  }
}
  • Related