简体   繁体   English

访问从外部库导入的 React 元素的函数

[英]Accessing a function of a React element imported from external library

I have a library of React components (we'll call it MyLibrary) that is transpiled with babel, exported as an NPM package, and made available to another repo (MyProject).我有一个 React 组件库(我们将其称为 MyLibrary),它使用 babel 进行转换,导出为 NPM 包,并提供给另一个存储库(MyProject)。 Within MyLibrary, I have a component TextField that makes the following function available:在 MyLibrary 中,我有一个组件 TextField,它使以下功能可用:

export default class TextField extends Component {

...

    getValue() {
        return this.state.value;
    }
}

The getValue function is properly bound to this in the defined class, and this function is available when I call it from other components inside of MyLibrary. getValue函数在定义的类中正确绑定到this ,当我从 MyLibrary 中的其他组件调用它时,这个函数可用。

However, when I am working in MyProject instead, and I have run npm install MyLibrary and imported TextField like so:但是,当我在 MyProject 中工作时,我已经运行npm install MyLibrary并导入了 TextField,如下所示:

import { TextField } from 'MyLibrary';

...

render() {
    this.field = <TextField id="testField" />;
    return field;
}

Elsewhere in the code, when I attempt to access the exported function like so:在代码的其他地方,当我尝试像这样访问导出的函数时:

console.log('Retrieving the value of the text field:', this.field.getValue());

I get the following error message:我收到以下错误消息:

Uncaught TypeError: field.getValue is not a function

Displaying the properties of the field variable in the console log, I see the following:在控制台日志中显示field变量的属性,我看到以下内容:

$$typeof: Symbol(react.element)
key: null
props: {id: "testField", labelText: "", invalidMessage: "", placeholder: "", spellCheck: false, …}
ref: null
type: ƒ TextField(props)
_owner: FiberNode {tag: 2, key: null, type: ƒ, stateNode: AddEditForm, return: FiberNode, …}
_store: {validated: false}
_self: null
_source: null
__proto__: Object

It seems that getValue is not available as a function at all, even though it is exported as part of the TextField class.似乎getValue根本不能作为函数使用,即使它作为 TextField 类的一部分导出。 It appears that my field variable is recognized in the code as a Symbol type rather than as a TextField type.看起来我的field变量在代码中被识别为 Symbol 类型而不是 TextField 类型。 Is there a way to retrieve the TextField instance directly?有没有办法直接检索 TextField 实例? Or is there some way I can otherwise avoid this discontinuity?或者有什么方法可以避免这种不连续性?

There is no way that you can access the function defined in a component within React context.您无法访问 React 上下文中组件中定义的函数。

The best solution would be to have a function that sets the value inside MyProject and use it as a prop for your TextField component.最好的解决方案是使用一个函数来设置MyProject的值,并将其用作TextField组件的道具。 Something like this:像这样的东西:

// MyLibrary/TextField.js

class TextField extends Component {
    constructor (props) {
        super(props);

        this.state = {
            value: null
        };
        this.setValue = this.setValue.bind(this);
    }

    setValue (value) {
        this.setState({ value }, () => this.props.setValue(value));
    }

    ...
}

// MyProject
...

setValue(value) {
    this.setState({ value })
}

render() {
    return (
        <TextField
            setValue={this.setValue} />
    );
}

You can also remove value from the state of TextField as you have always access to it within the state MyProject , and you can always read the value from the props of TextField after providing TextField with the value prop read from the state of MyProject : <TextField setValue={this.setValue} value={this.state.value} /> .您还可以删除value从状态TextField为你所要的状态中访问它MyProject ,并且可以随时读取的道具价值TextField提供完善TextFieldvalue道具从状态看MyProject<TextField setValue={this.setValue} value={this.state.value} />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM