如何在不使用React Native中的'this'關鍵字的情況下訪問元素的屬性?我有一個函數,它的父類本身綁定爲「this」,但我想訪問被單擊的元素的屬性。這裏的代碼 -如何在Click事件中獲取React Native中的元素屬性
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)}/>
);
}
}
這看起來很亂。 React是否在某處記錄/推薦/背書? – hippietrail
它似乎在最新版本中被更改。 'react-native/Libraries/Renderer/src/renderers/native/ReactNativeComponentTree' - >'react-native/Libraries/Renderer/shims/ReactNativeComponentTree','._currentElement.props' - >'.memoizedProps' – mpyw
@hippietrail這是hacky **非常慢**。我們最好避免這個提示。 – mpyw