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). Within MyLibrary, I have a component TextField that makes the following function available:
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.
However, when I am working in MyProject instead, and I have run npm install MyLibrary
and imported TextField like so:
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:
$$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. It appears that my field
variable is recognized in the code as a Symbol type rather than as a TextField type. Is there a way to retrieve the TextField instance directly? Or is there some way I can otherwise avoid this discontinuity?
reactjs babeljs es6-class
add a comment |
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). Within MyLibrary, I have a component TextField that makes the following function available:
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.
However, when I am working in MyProject instead, and I have run npm install MyLibrary
and imported TextField like so:
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:
$$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. It appears that my field
variable is recognized in the code as a Symbol type rather than as a TextField type. Is there a way to retrieve the TextField instance directly? Or is there some way I can otherwise avoid this discontinuity?
reactjs babeljs es6-class
add a comment |
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). Within MyLibrary, I have a component TextField that makes the following function available:
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.
However, when I am working in MyProject instead, and I have run npm install MyLibrary
and imported TextField like so:
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:
$$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. It appears that my field
variable is recognized in the code as a Symbol type rather than as a TextField type. Is there a way to retrieve the TextField instance directly? Or is there some way I can otherwise avoid this discontinuity?
reactjs babeljs es6-class
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). Within MyLibrary, I have a component TextField that makes the following function available:
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.
However, when I am working in MyProject instead, and I have run npm install MyLibrary
and imported TextField like so:
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:
$$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. It appears that my field
variable is recognized in the code as a Symbol type rather than as a TextField type. Is there a way to retrieve the TextField instance directly? Or is there some way I can otherwise avoid this discontinuity?
reactjs babeljs es6-class
reactjs babeljs es6-class
asked Nov 14 '18 at 0:49
MarishaMarisha
7910
7910
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
There is no way that you can access the function defined in a component within React context.
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. 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} />
.
Thank you for the information. Can you provide any additional details on why the class method is available from within its own repo, but is not available when importing it from a library? That seems counterintuitive to the way classes are meant to work, at least in any other language.
– Marisha
Nov 14 '18 at 16:55
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291615%2faccessing-a-function-of-a-react-element-imported-from-external-library%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
There is no way that you can access the function defined in a component within React context.
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. 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} />
.
Thank you for the information. Can you provide any additional details on why the class method is available from within its own repo, but is not available when importing it from a library? That seems counterintuitive to the way classes are meant to work, at least in any other language.
– Marisha
Nov 14 '18 at 16:55
add a comment |
There is no way that you can access the function defined in a component within React context.
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. 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} />
.
Thank you for the information. Can you provide any additional details on why the class method is available from within its own repo, but is not available when importing it from a library? That seems counterintuitive to the way classes are meant to work, at least in any other language.
– Marisha
Nov 14 '18 at 16:55
add a comment |
There is no way that you can access the function defined in a component within React context.
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. 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} />
.
There is no way that you can access the function defined in a component within React context.
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. 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} />
.
answered Nov 14 '18 at 1:23
MasiousMasious
354214
354214
Thank you for the information. Can you provide any additional details on why the class method is available from within its own repo, but is not available when importing it from a library? That seems counterintuitive to the way classes are meant to work, at least in any other language.
– Marisha
Nov 14 '18 at 16:55
add a comment |
Thank you for the information. Can you provide any additional details on why the class method is available from within its own repo, but is not available when importing it from a library? That seems counterintuitive to the way classes are meant to work, at least in any other language.
– Marisha
Nov 14 '18 at 16:55
Thank you for the information. Can you provide any additional details on why the class method is available from within its own repo, but is not available when importing it from a library? That seems counterintuitive to the way classes are meant to work, at least in any other language.
– Marisha
Nov 14 '18 at 16:55
Thank you for the information. Can you provide any additional details on why the class method is available from within its own repo, but is not available when importing it from a library? That seems counterintuitive to the way classes are meant to work, at least in any other language.
– Marisha
Nov 14 '18 at 16:55
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291615%2faccessing-a-function-of-a-react-element-imported-from-external-library%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown