react-native – 如何在Click事件中获取React Native中的Element属性

前端之家收集整理的这篇文章主要介绍了react-native – 如何在Click事件中获取React Native中的Element属性前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何在不使用React Native中的’this’关键字的情况下访问元素的属性?我有一个函数,父类本身绑定为’this’但我想访问被单击的元素的属性.这是代码
import {Circle} from 'react-native-svg';
export default App extends Component {
  constructor(props) {
  super(props);
  this.state = {activeX: null}
 }

 handleTouch(event) {
   const x = event.target.cx; //How to access "cx" property here?
   this.setState({ activeX: x });
 }

 render() {
   return (
     <Circle cx='10' cy='10' r='5' onPress={this.handleTouch.bind(this)}/>
     <Circle cx='20' cy='20' r='5' onPress={this.handleTouch.bind(this)}/>
   );
 }
}
尝试这个
import {Circle} from 'react-native-svg';
export default App extends Component {
  constructor(props) {
  super(props);
  this.state = {
    activeX: null,cx: 10
  }
 }

 handleTouch = () => {
   const x = this.state.cx
   this.setState({ activeX: x });
 }

 render() {
   return (
     <Circle cx={this.state.cx} cy='10' r='5' onPress={this.handleTouch}/>

   );
 }
}

猜你在找的React相关文章