目标:创建一个可重用的OptionFan组件,允许子组件作为ChildButton组件。问题:无法访问OptionFan(父)组件方法"showOptions()“中的儿童按钮方法”showOptions()“
在选项扇组件中:
showOptions = () => {
let animations = this.props.children.map((child, i) => {
this.refs.child.flyOut();
});
Animated.stagger(this.props.staggerDelay, animations).start();
}
renderOptions = () => {
return this.props.children.map((child, i) => {
return <ChildButton ref={child} siblings={this.props.children.length} key={i} icon={} number={i} size={} />
})
}在ChildButton组件中:
componentDidMount() {
this.props.ref(this);
}
flyOut = () => {
const {number, size} = this.state;
let offset = this.findChildCoordinates(number);
Animated.timing(
this.state.move,
{toValue: offset}
).start();
}所需的方法在代码建议中是不可访问的,在我的方法中什么是不正确的?
发布于 2018-05-23 03:24:59
你需要把它们绑定到你的课堂上。更改您的optionFan组件如下:
Option Fan:
showOptions = () => {
let animations = this.props.children.map((child, i) => {
this.child.flyOut();
});
Animated.stagger(this.props.staggerDelay, animations).start();
}
renderOptions = () => {
return this.props.children.map(i => {
return <ChildButton ref={ Ref =>(this.child = Ref)} siblings={this.props.children.length} key={i} icon={} number={i} size={} />
})
}有关更多参考,请参见此https://github.com/kriasoft/react-starter-kit/issues/909#issuecomment-252969542
https://stackoverflow.com/questions/50478209
复制相似问题