首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >本机:在父组件中访问子组件函数

本机:在父组件中访问子组件函数
EN

Stack Overflow用户
提问于 2018-05-23 00:29:01
回答 1查看 2.9K关注 0票数 0

目标:创建一个可重用的OptionFan组件,允许子组件作为ChildButton组件。问题:无法访问OptionFan(父)组件方法"showOptions()“中的儿童按钮方法”showOptions()“

在选项扇组件中:

代码语言:javascript
复制
 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组件中:

代码语言:javascript
复制
componentDidMount() {
        this.props.ref(this);
    }

flyOut = () => {
        const {number, size} = this.state;
        let offset = this.findChildCoordinates(number);
        Animated.timing(
            this.state.move,
            {toValue: offset}
        ).start();
    }

所需的方法在代码建议中是不可访问的,在我的方法中什么是不正确的?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-23 03:24:59

你需要把它们绑定到你的课堂上。更改您的optionFan组件如下:

Option Fan:

代码语言:javascript
复制
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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50478209

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档