for eg.
Child.js
//assume there is s as styles object and used in this component
class Child extends Component {
render() {
return (
<h1 ref={(ref)=>{this.ref = ref}}>Hello</h1>
);
}
}
export default withStyles(s)(Child);
Parent.js
class Parent extends Component {
onClick() {
console.log(this.child) // here access the ref
console.log(this.child.ref) // undefined
}
render() {
return (
<div>
<Child ref={child => this.child = child} />
<button onClick={this.onClick.bind(this)}>Click</button>
</div>
);
}
}
由于样式的改变,整个父组件中的this.child ref也发生了改变。请帮我想出一个解决方法,但请勿放弃使用withStyles。
<Child innerRef={child => this.child[id] = child} />
- Shubham Khatri