Pass parameter from external js to react prop
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
add a comment |
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
add a comment |
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
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
javascript reactjs
asked Nov 19 '18 at 7:36
JasonJason
292
292
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
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.
add a comment |
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.
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
add a comment |
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
add a comment |
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/
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
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%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
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 19 '18 at 7:54
Unsolved CypherUnsolved Cypher
495314
495314
add a comment |
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 19 '18 at 10:38
JasonJason
292
292
add a comment |
add a comment |
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/
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
add a comment |
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/
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
add a comment |
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/
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/
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
add a comment |
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
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%2f53370176%2fpass-parameter-from-external-js-to-react-prop%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