如何将这个不受控制的组件转换为反应挂钩(并避免对输入字段进行多次渲染):
class MyForm extends React.Component {
constructor(props) {
super(props);
this.input = React.createRef();
}
render() {
const {onInputChanged} = this.props;
return (
<form>
<input type="text" ref={this.input} onChange={onInputChanged}/>
</form>
);
}
}
答案 0 :(得分:0)
答案 1 :(得分:0)
如果要将组件转换为功能组件并使用钩子,可以这样做:
import React, { useRef,useEffect } from 'react';
useEffect(() => {
},['the props you write here will trigger the re-render when they change'])
const MyForm = (props) => {
const input = useRef(null);
return (
<form>
<input type="text" ref={input} onChange={props.onInputChanged} />
</form>
);
}
export default MyForm;
答案 2 :(得分:0)
import React, { useRef } from 'react';
export const MyForm = (props) => {
const input = useRef(null);
return (
<form>
<input type="text" ref={input} onChange={props.onInputChanged} />
</form>
);
}