Accessing a function of a React element imported from external library












1















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?










share|improve this question



























    1















    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?










    share|improve this question

























      1












      1








      1








      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?










      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 0:49









      MarishaMarisha

      7910




      7910
























          1 Answer
          1






          active

          oldest

          votes


















          1














          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} />.






          share|improve this answer
























          • 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











          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
          });


          }
          });














          draft saved

          draft discarded


















          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









          1














          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} />.






          share|improve this answer
























          • 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
















          1














          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} />.






          share|improve this answer
























          • 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














          1












          1








          1







          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} />.






          share|improve this answer













          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} />.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          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



















          • 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


















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Xamarin.iOS Cant Deploy on Iphone

          Glorious Revolution

          Dulmage-Mendelsohn matrix decomposition in Python