使用函数中组件的上下文

时间:2018-06-06 14:01:18

标签: javascript reactjs undefined

我遇到了问题 - 我尝试使用 reactScrollbar ,但该软件包在上下文中有{ "status": "sent", "emailSubject": "test subject", "documents": [{ "documentId": "1", "name": "test.html", "documentBase64": "PCFkb2N0eXBlIGh0bWw+PGh0bWw+PGhlYWQ+PC9oZWFkPjxib2R5PnRlc3Q8L2JvZHk+PC9odG1sPg==", "fileExtension": "html" }], "recipients": { "signers": [{ "name": "Test Name", "email": "test@perlito.ru", "recipientId": "1", "routingOrder": "1", "tabs": { "signHereTabs": [{ "documentId": "1", "pageNumber": "1", "recipientId": "1", "xPosition": "500", "yPosition": "500" }] } }] } } 函数。

我有这个:

scrollTop()

我想做类似的事情:

<ScrollArea
   speed={0.5}
   className="scroll-container"
   contentClassName="container"
   smoothScrolling={true}
   horizontal={false}
   onScroll={(value) => this.handleScrollWheel(value)} >

上下文未定义。任何解决方案?

1 个答案:

答案 0 :(得分:1)

您需要注册ScrollArea的context

来自DOCS

class App extends React.Component {
    render(){
        return (
            <ScrollArea>
                <Content />
            </ScrollArea>
        );
    }
}

class Content extends React.Component {
    render(){
        return (
            <div onClick={this.handleSomeAction.bind(this)}> Some long content </div>
        );
    }

    handleSomeAction(){
        this.context.scrollArea.refresh();
    }
}

Content.contextTypes = {
    scrollArea: React.PropTypes.object
};

请注意,这是使用the legacy context API of react