我有一个带有testID字段的按钮。
有没有办法通过编程的方式点击一个给定testID的按钮?
<Button testID='someButton' onPress={someButtonClick}/>
有没有办法通过编程的方式点击一个给定testID的按钮?
<Button testID='someButton' onPress={someButtonClick}/>
import React from "react"
import { View, Button } from 'react-native';
const someButtonClick = () => {
console.log("button was pressed");
}
export class PressButton extends React.Component {
constructor(props) {
super(props);
this.state = {}
}
clickButton() {
this.button.props.onPress();
}
render() {
return (
<View>
<Button title="another button"
onPress={() => { this.clickButton()}}
/>
<Button title="some button" testID='someButton'
onPress={() => { someButtonClick() }}
ref={(button) => {this.button = button; }}
/>
</View>
)
}
}
<Button id={buttonId}
onClick={e => console.log(e.target)}
ref={(button) => { this.myButton = button; }}
// Or something like this:
ref={component => this.myButton = component}
/>
myFunction = () => {
this.myButton.click()
}
ref={component => this.myButton1 = component}
和 ref={component => this.myButton2 = component}
。 - A. Larssonconst uploadbtn = useRef();
<Button
ref={uploadbtn}
buttonStyle={{ width: 0, height: 0, opacity: 0, display: "none" }}
onPress={pickImage}
/>
const test = () => {
uploadbtn.current.handleOnPress();
};
在渲染代码中尝试这个。
<div className="container" hidden >
<button onClick={this.submit} ref={(button) => { this.btn = button }} />
</div>
在函数中
btn = () => {
this.btn.click()
}
在 else 代码中
this.btn()
在 render() 函数中
<button type='button' onClick="{this.btnClickFunc.bind(this)}">buton</button>
和
function btnClickFunc(e){
var value = e.target.value;
}
希望对你有所帮助