Pass parameter from external js to react prop












0















Trying to convert an in-app webpage to React. The app webview automatically calls a Javascript function to pass an access token to the webpage. So is it possible to use the same existing function to pass the token variable and store it as a React prop?



HTML



<div id="app"></div>
<script>
function setToken(token){
//set token to App token prop
}
</script>


JS



class App extends React.Component {
constructor(props) {
super(props);
this.state = {
token: "",
}
}

render() {
return (
<div>
<p>{this.state.token}</p>
</div>
);
}
}

ReactDOM.render(<App />, document.querySelector("#app"))


https://jsfiddle.net/wzovs2gy/










share|improve this question



























    0















    Trying to convert an in-app webpage to React. The app webview automatically calls a Javascript function to pass an access token to the webpage. So is it possible to use the same existing function to pass the token variable and store it as a React prop?



    HTML



    <div id="app"></div>
    <script>
    function setToken(token){
    //set token to App token prop
    }
    </script>


    JS



    class App extends React.Component {
    constructor(props) {
    super(props);
    this.state = {
    token: "",
    }
    }

    render() {
    return (
    <div>
    <p>{this.state.token}</p>
    </div>
    );
    }
    }

    ReactDOM.render(<App />, document.querySelector("#app"))


    https://jsfiddle.net/wzovs2gy/










    share|improve this question

























      0












      0








      0








      Trying to convert an in-app webpage to React. The app webview automatically calls a Javascript function to pass an access token to the webpage. So is it possible to use the same existing function to pass the token variable and store it as a React prop?



      HTML



      <div id="app"></div>
      <script>
      function setToken(token){
      //set token to App token prop
      }
      </script>


      JS



      class App extends React.Component {
      constructor(props) {
      super(props);
      this.state = {
      token: "",
      }
      }

      render() {
      return (
      <div>
      <p>{this.state.token}</p>
      </div>
      );
      }
      }

      ReactDOM.render(<App />, document.querySelector("#app"))


      https://jsfiddle.net/wzovs2gy/










      share|improve this question














      Trying to convert an in-app webpage to React. The app webview automatically calls a Javascript function to pass an access token to the webpage. So is it possible to use the same existing function to pass the token variable and store it as a React prop?



      HTML



      <div id="app"></div>
      <script>
      function setToken(token){
      //set token to App token prop
      }
      </script>


      JS



      class App extends React.Component {
      constructor(props) {
      super(props);
      this.state = {
      token: "",
      }
      }

      render() {
      return (
      <div>
      <p>{this.state.token}</p>
      </div>
      );
      }
      }

      ReactDOM.render(<App />, document.querySelector("#app"))


      https://jsfiddle.net/wzovs2gy/







      javascript reactjs






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 19 '18 at 7:36









      JasonJason

      292




      292
























          4 Answers
          4






          active

          oldest

          votes


















          0














          You could make it a callback function, so it will be given another function to call when it has a result (meaning it would be passed a function from within React that would then update the state/props. If you're looking to do this with a minimal number of changes, I would suggest that you have the function store the token in a variable, and then later query this variable in React.






          share|improve this answer































            0














            It's impossible - or at least impractical - to access React component instance outside React application. This should be done in opposite way, the component should expose global function:



            class App extends React.Component {
            constructor(props) {
            super(props);
            this.state = {
            token: "",
            }
            }

            componentDidMount() {
            window.setToken = token => {
            this.setState({ token });
            }
            }

            ...
            }


            setToken shouldn't be called before React component initialization. Depending on where setToken is called, it may be beneficial to wrap the code that uses it with React application instead and avoid using globals.






            share|improve this answer
























            • the setToken is called by the app once the webpage has finished loading. I just need a way to define the setToken function which can be called by the app whether its inside or outside React.

              – Jason
              Nov 19 '18 at 10:06



















            0














            Thanks for your help guys, managed to figure out a solution (while not elegant does the job). I declared the ReactDom.Render() as a variable which allows me to reference the function within the React Parent in the Vanilla JS.



            JS



            var ReactDom = ReactDOM.render(<App />, document.querySelector("#app"));


            HTML



            function setToken(token){
            ReactDom.setNewToken(token);
            }


            https://jsfiddle.net/wzovs2gy/2






            share|improve this answer































              0














              I have modified the fiddle:



              class App extends React.Component {
              constructor(props) {
              super(props);
              this.state = {
              token: "test",
              }
              }
              componentDidMount(){
              setToken(this.state.token);
              }

              render() {
              return (
              <div>
              <p>{this.state.token}</p>
              </div>
              );
              }
              }

              ReactDOM.render(<App />, document.querySelector("#app"))


              https://jsfiddle.net/wzovs2gy/1/






              share|improve this answer


























              • Thanks, but I'm trying to do the reverse. The app calls the setToken function on the webpage once the webpage has finished loading.

                – Jason
                Nov 19 '18 at 9:59











              • Means setToken value will set on the state??

                – pixellab
                Nov 19 '18 at 12:18











              • update again: jsfiddle.net/wzovs2gy/3

                – pixellab
                Nov 19 '18 at 12:21











              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%2f53370176%2fpass-parameter-from-external-js-to-react-prop%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              4 Answers
              4






              active

              oldest

              votes








              4 Answers
              4






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              You could make it a callback function, so it will be given another function to call when it has a result (meaning it would be passed a function from within React that would then update the state/props. If you're looking to do this with a minimal number of changes, I would suggest that you have the function store the token in a variable, and then later query this variable in React.






              share|improve this answer




























                0














                You could make it a callback function, so it will be given another function to call when it has a result (meaning it would be passed a function from within React that would then update the state/props. If you're looking to do this with a minimal number of changes, I would suggest that you have the function store the token in a variable, and then later query this variable in React.






                share|improve this answer


























                  0












                  0








                  0







                  You could make it a callback function, so it will be given another function to call when it has a result (meaning it would be passed a function from within React that would then update the state/props. If you're looking to do this with a minimal number of changes, I would suggest that you have the function store the token in a variable, and then later query this variable in React.






                  share|improve this answer













                  You could make it a callback function, so it will be given another function to call when it has a result (meaning it would be passed a function from within React that would then update the state/props. If you're looking to do this with a minimal number of changes, I would suggest that you have the function store the token in a variable, and then later query this variable in React.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 19 '18 at 7:54









                  Unsolved CypherUnsolved Cypher

                  495314




                  495314

























                      0














                      It's impossible - or at least impractical - to access React component instance outside React application. This should be done in opposite way, the component should expose global function:



                      class App extends React.Component {
                      constructor(props) {
                      super(props);
                      this.state = {
                      token: "",
                      }
                      }

                      componentDidMount() {
                      window.setToken = token => {
                      this.setState({ token });
                      }
                      }

                      ...
                      }


                      setToken shouldn't be called before React component initialization. Depending on where setToken is called, it may be beneficial to wrap the code that uses it with React application instead and avoid using globals.






                      share|improve this answer
























                      • the setToken is called by the app once the webpage has finished loading. I just need a way to define the setToken function which can be called by the app whether its inside or outside React.

                        – Jason
                        Nov 19 '18 at 10:06
















                      0














                      It's impossible - or at least impractical - to access React component instance outside React application. This should be done in opposite way, the component should expose global function:



                      class App extends React.Component {
                      constructor(props) {
                      super(props);
                      this.state = {
                      token: "",
                      }
                      }

                      componentDidMount() {
                      window.setToken = token => {
                      this.setState({ token });
                      }
                      }

                      ...
                      }


                      setToken shouldn't be called before React component initialization. Depending on where setToken is called, it may be beneficial to wrap the code that uses it with React application instead and avoid using globals.






                      share|improve this answer
























                      • the setToken is called by the app once the webpage has finished loading. I just need a way to define the setToken function which can be called by the app whether its inside or outside React.

                        – Jason
                        Nov 19 '18 at 10:06














                      0












                      0








                      0







                      It's impossible - or at least impractical - to access React component instance outside React application. This should be done in opposite way, the component should expose global function:



                      class App extends React.Component {
                      constructor(props) {
                      super(props);
                      this.state = {
                      token: "",
                      }
                      }

                      componentDidMount() {
                      window.setToken = token => {
                      this.setState({ token });
                      }
                      }

                      ...
                      }


                      setToken shouldn't be called before React component initialization. Depending on where setToken is called, it may be beneficial to wrap the code that uses it with React application instead and avoid using globals.






                      share|improve this answer













                      It's impossible - or at least impractical - to access React component instance outside React application. This should be done in opposite way, the component should expose global function:



                      class App extends React.Component {
                      constructor(props) {
                      super(props);
                      this.state = {
                      token: "",
                      }
                      }

                      componentDidMount() {
                      window.setToken = token => {
                      this.setState({ token });
                      }
                      }

                      ...
                      }


                      setToken shouldn't be called before React component initialization. Depending on where setToken is called, it may be beneficial to wrap the code that uses it with React application instead and avoid using globals.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 19 '18 at 8:04









                      estusestus

                      71.9k22106222




                      71.9k22106222













                      • the setToken is called by the app once the webpage has finished loading. I just need a way to define the setToken function which can be called by the app whether its inside or outside React.

                        – Jason
                        Nov 19 '18 at 10:06



















                      • the setToken is called by the app once the webpage has finished loading. I just need a way to define the setToken function which can be called by the app whether its inside or outside React.

                        – Jason
                        Nov 19 '18 at 10:06

















                      the setToken is called by the app once the webpage has finished loading. I just need a way to define the setToken function which can be called by the app whether its inside or outside React.

                      – Jason
                      Nov 19 '18 at 10:06





                      the setToken is called by the app once the webpage has finished loading. I just need a way to define the setToken function which can be called by the app whether its inside or outside React.

                      – Jason
                      Nov 19 '18 at 10:06











                      0














                      Thanks for your help guys, managed to figure out a solution (while not elegant does the job). I declared the ReactDom.Render() as a variable which allows me to reference the function within the React Parent in the Vanilla JS.



                      JS



                      var ReactDom = ReactDOM.render(<App />, document.querySelector("#app"));


                      HTML



                      function setToken(token){
                      ReactDom.setNewToken(token);
                      }


                      https://jsfiddle.net/wzovs2gy/2






                      share|improve this answer




























                        0














                        Thanks for your help guys, managed to figure out a solution (while not elegant does the job). I declared the ReactDom.Render() as a variable which allows me to reference the function within the React Parent in the Vanilla JS.



                        JS



                        var ReactDom = ReactDOM.render(<App />, document.querySelector("#app"));


                        HTML



                        function setToken(token){
                        ReactDom.setNewToken(token);
                        }


                        https://jsfiddle.net/wzovs2gy/2






                        share|improve this answer


























                          0












                          0








                          0







                          Thanks for your help guys, managed to figure out a solution (while not elegant does the job). I declared the ReactDom.Render() as a variable which allows me to reference the function within the React Parent in the Vanilla JS.



                          JS



                          var ReactDom = ReactDOM.render(<App />, document.querySelector("#app"));


                          HTML



                          function setToken(token){
                          ReactDom.setNewToken(token);
                          }


                          https://jsfiddle.net/wzovs2gy/2






                          share|improve this answer













                          Thanks for your help guys, managed to figure out a solution (while not elegant does the job). I declared the ReactDom.Render() as a variable which allows me to reference the function within the React Parent in the Vanilla JS.



                          JS



                          var ReactDom = ReactDOM.render(<App />, document.querySelector("#app"));


                          HTML



                          function setToken(token){
                          ReactDom.setNewToken(token);
                          }


                          https://jsfiddle.net/wzovs2gy/2







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 19 '18 at 10:38









                          JasonJason

                          292




                          292























                              0














                              I have modified the fiddle:



                              class App extends React.Component {
                              constructor(props) {
                              super(props);
                              this.state = {
                              token: "test",
                              }
                              }
                              componentDidMount(){
                              setToken(this.state.token);
                              }

                              render() {
                              return (
                              <div>
                              <p>{this.state.token}</p>
                              </div>
                              );
                              }
                              }

                              ReactDOM.render(<App />, document.querySelector("#app"))


                              https://jsfiddle.net/wzovs2gy/1/






                              share|improve this answer


























                              • Thanks, but I'm trying to do the reverse. The app calls the setToken function on the webpage once the webpage has finished loading.

                                – Jason
                                Nov 19 '18 at 9:59











                              • Means setToken value will set on the state??

                                – pixellab
                                Nov 19 '18 at 12:18











                              • update again: jsfiddle.net/wzovs2gy/3

                                – pixellab
                                Nov 19 '18 at 12:21
















                              0














                              I have modified the fiddle:



                              class App extends React.Component {
                              constructor(props) {
                              super(props);
                              this.state = {
                              token: "test",
                              }
                              }
                              componentDidMount(){
                              setToken(this.state.token);
                              }

                              render() {
                              return (
                              <div>
                              <p>{this.state.token}</p>
                              </div>
                              );
                              }
                              }

                              ReactDOM.render(<App />, document.querySelector("#app"))


                              https://jsfiddle.net/wzovs2gy/1/






                              share|improve this answer


























                              • Thanks, but I'm trying to do the reverse. The app calls the setToken function on the webpage once the webpage has finished loading.

                                – Jason
                                Nov 19 '18 at 9:59











                              • Means setToken value will set on the state??

                                – pixellab
                                Nov 19 '18 at 12:18











                              • update again: jsfiddle.net/wzovs2gy/3

                                – pixellab
                                Nov 19 '18 at 12:21














                              0












                              0








                              0







                              I have modified the fiddle:



                              class App extends React.Component {
                              constructor(props) {
                              super(props);
                              this.state = {
                              token: "test",
                              }
                              }
                              componentDidMount(){
                              setToken(this.state.token);
                              }

                              render() {
                              return (
                              <div>
                              <p>{this.state.token}</p>
                              </div>
                              );
                              }
                              }

                              ReactDOM.render(<App />, document.querySelector("#app"))


                              https://jsfiddle.net/wzovs2gy/1/






                              share|improve this answer















                              I have modified the fiddle:



                              class App extends React.Component {
                              constructor(props) {
                              super(props);
                              this.state = {
                              token: "test",
                              }
                              }
                              componentDidMount(){
                              setToken(this.state.token);
                              }

                              render() {
                              return (
                              <div>
                              <p>{this.state.token}</p>
                              </div>
                              );
                              }
                              }

                              ReactDOM.render(<App />, document.querySelector("#app"))


                              https://jsfiddle.net/wzovs2gy/1/







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Nov 19 '18 at 10:57









                              bfontaine

                              9,02974272




                              9,02974272










                              answered Nov 19 '18 at 9:02









                              pixellabpixellab

                              340210




                              340210













                              • Thanks, but I'm trying to do the reverse. The app calls the setToken function on the webpage once the webpage has finished loading.

                                – Jason
                                Nov 19 '18 at 9:59











                              • Means setToken value will set on the state??

                                – pixellab
                                Nov 19 '18 at 12:18











                              • update again: jsfiddle.net/wzovs2gy/3

                                – pixellab
                                Nov 19 '18 at 12:21



















                              • Thanks, but I'm trying to do the reverse. The app calls the setToken function on the webpage once the webpage has finished loading.

                                – Jason
                                Nov 19 '18 at 9:59











                              • Means setToken value will set on the state??

                                – pixellab
                                Nov 19 '18 at 12:18











                              • update again: jsfiddle.net/wzovs2gy/3

                                – pixellab
                                Nov 19 '18 at 12:21

















                              Thanks, but I'm trying to do the reverse. The app calls the setToken function on the webpage once the webpage has finished loading.

                              – Jason
                              Nov 19 '18 at 9:59





                              Thanks, but I'm trying to do the reverse. The app calls the setToken function on the webpage once the webpage has finished loading.

                              – Jason
                              Nov 19 '18 at 9:59













                              Means setToken value will set on the state??

                              – pixellab
                              Nov 19 '18 at 12:18





                              Means setToken value will set on the state??

                              – pixellab
                              Nov 19 '18 at 12:18













                              update again: jsfiddle.net/wzovs2gy/3

                              – pixellab
                              Nov 19 '18 at 12:21





                              update again: jsfiddle.net/wzovs2gy/3

                              – pixellab
                              Nov 19 '18 at 12:21


















                              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%2f53370176%2fpass-parameter-from-external-js-to-react-prop%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







                              這個網誌中的熱門文章

                              Academy of Television Arts & Sciences

                              L'Équipe

                              1995 France bombings