我有一個智能組件和一個愚蠢組件,我需要引用位於我的智能組件中的轉儲組件中的元素: can我通過它道具或?React - 從啞組件(子)傳遞給智能組件(父)
Dumb:
export default (props)=>{
return(
<input type='number' ref='element'}/>
);}
Smart:
class Parent extends Component {
constructor(props) {
super(props);
}
componentDidMount() {
const node = this.refs.element; // undefined
}
render(){
return <Dumb { ...this.props }/>
}
}
這個方法很好,謝謝! :) – Nick1R1
我寧願使用回調,而不是將ref傳遞給children組件,因爲您持有對父組件的引用。對於簡單的組件工作正常,但對於大/複雜的組件,根據最佳實踐,您應該使用回調。 –