在React.js中获取合适的输入变量的值可以通过以下几种方式实现:
class MyForm extends React.Component {
constructor(props) {
super(props);
this.state = {
inputValue: ''
};
}
handleChange(event) {
this.setState({ inputValue: event.target.value });
}
render() {
return (
<input
type="text"
value={this.state.inputValue}
onChange={this.handleChange.bind(this)}
/>
);
}
}
class MyForm extends React.Component {
constructor(props) {
super(props);
this.inputRef = React.createRef();
}
handleSubmit(event) {
event.preventDefault();
console.log(this.inputRef.current.value);
}
render() {
return (
<form onSubmit={this.handleSubmit.bind(this)}>
<input type="text" ref={this.inputRef} />
<button type="submit">Submit</button>
</form>
);
}
}
以上是在React.js中获取合适的输入变量值的几种常见方式。根据具体的需求和场景,选择合适的方式来获取输入变量的值。
领取专属 10元无门槛券
手把手带您无忧上云