TypeError: Cannot read property 'value' of null | reactjs |
I've 2 textboxes and 1 button as material UI components.
</p>
<TextField id="chatidField" />
<br />
<p>
<code>Enter Your Name:</code>
<hr />
</p>
<TextField id="nameField" />
<br />
<br />
<Button
variant="contained"
color="secondary"
onClick={addToFirebase()}
>
Get Your Token
</Button>
the function that onClick calls is as follows:
function addToFirebase() {
var chatid = document.getElementById("chatidField").value;
var name = document.getElementById("nameField").value;
console.log(chatid, name);
}
I keep getting the following error:
TypeError: Cannot read property 'value' of null
8 | var chatid = document.getElementById("chatidField").value;
9 | var name = document.getElementById("nameField").value;
full code: https://codesandbox.io/s/pqmrn4x
javascript html css reactjs material-ui
add a comment |
I've 2 textboxes and 1 button as material UI components.
</p>
<TextField id="chatidField" />
<br />
<p>
<code>Enter Your Name:</code>
<hr />
</p>
<TextField id="nameField" />
<br />
<br />
<Button
variant="contained"
color="secondary"
onClick={addToFirebase()}
>
Get Your Token
</Button>
the function that onClick calls is as follows:
function addToFirebase() {
var chatid = document.getElementById("chatidField").value;
var name = document.getElementById("nameField").value;
console.log(chatid, name);
}
I keep getting the following error:
TypeError: Cannot read property 'value' of null
8 | var chatid = document.getElementById("chatidField").value;
9 | var name = document.getElementById("nameField").value;
full code: https://codesandbox.io/s/pqmrn4x
javascript html css reactjs material-ui
Pass function reference rather than call in onCick={addToFirebase}
– Diljohn5741
Nov 23 '18 at 5:18
add a comment |
I've 2 textboxes and 1 button as material UI components.
</p>
<TextField id="chatidField" />
<br />
<p>
<code>Enter Your Name:</code>
<hr />
</p>
<TextField id="nameField" />
<br />
<br />
<Button
variant="contained"
color="secondary"
onClick={addToFirebase()}
>
Get Your Token
</Button>
the function that onClick calls is as follows:
function addToFirebase() {
var chatid = document.getElementById("chatidField").value;
var name = document.getElementById("nameField").value;
console.log(chatid, name);
}
I keep getting the following error:
TypeError: Cannot read property 'value' of null
8 | var chatid = document.getElementById("chatidField").value;
9 | var name = document.getElementById("nameField").value;
full code: https://codesandbox.io/s/pqmrn4x
javascript html css reactjs material-ui
I've 2 textboxes and 1 button as material UI components.
</p>
<TextField id="chatidField" />
<br />
<p>
<code>Enter Your Name:</code>
<hr />
</p>
<TextField id="nameField" />
<br />
<br />
<Button
variant="contained"
color="secondary"
onClick={addToFirebase()}
>
Get Your Token
</Button>
the function that onClick calls is as follows:
function addToFirebase() {
var chatid = document.getElementById("chatidField").value;
var name = document.getElementById("nameField").value;
console.log(chatid, name);
}
I keep getting the following error:
TypeError: Cannot read property 'value' of null
8 | var chatid = document.getElementById("chatidField").value;
9 | var name = document.getElementById("nameField").value;
full code: https://codesandbox.io/s/pqmrn4x
javascript html css reactjs material-ui
javascript html css reactjs material-ui
edited Nov 23 '18 at 5:19
sooraj viswanath
asked Nov 23 '18 at 5:14
sooraj viswanathsooraj viswanath
75
75
Pass function reference rather than call in onCick={addToFirebase}
– Diljohn5741
Nov 23 '18 at 5:18
add a comment |
Pass function reference rather than call in onCick={addToFirebase}
– Diljohn5741
Nov 23 '18 at 5:18
Pass function reference rather than call in onCick={addToFirebase}
– Diljohn5741
Nov 23 '18 at 5:18
Pass function reference rather than call in onCick={addToFirebase}
– Diljohn5741
Nov 23 '18 at 5:18
add a comment |
1 Answer
1
active
oldest
votes
You have an error in your onClick
handler, it should be addToFirebase
not addToFirebase()
:
<Button
variant="contained"
color="secondary"
onClick={addToFirebase}
>
When you include ()
, it will run the addToFirebase
function, and use the return value as the onClick
handler. In addition, if the addToFirebase()
function is a part of your class, you will likely need to use this.addToFirebase
instead.
2
@HemadriDasari They're null, because at the time thataddToFirebase()
is run, the actual DOM hasn't been rendered yet, so none of the elements with those ID's exist. After applying the onClick function correctly, when the button gets clicked, those elements should indeed exist in the DOM, and the error should go away.
– FrankerZ
Nov 23 '18 at 5:36
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%2f53440954%2ftypeerror-cannot-read-property-value-of-null-reactjs%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
You have an error in your onClick
handler, it should be addToFirebase
not addToFirebase()
:
<Button
variant="contained"
color="secondary"
onClick={addToFirebase}
>
When you include ()
, it will run the addToFirebase
function, and use the return value as the onClick
handler. In addition, if the addToFirebase()
function is a part of your class, you will likely need to use this.addToFirebase
instead.
2
@HemadriDasari They're null, because at the time thataddToFirebase()
is run, the actual DOM hasn't been rendered yet, so none of the elements with those ID's exist. After applying the onClick function correctly, when the button gets clicked, those elements should indeed exist in the DOM, and the error should go away.
– FrankerZ
Nov 23 '18 at 5:36
add a comment |
You have an error in your onClick
handler, it should be addToFirebase
not addToFirebase()
:
<Button
variant="contained"
color="secondary"
onClick={addToFirebase}
>
When you include ()
, it will run the addToFirebase
function, and use the return value as the onClick
handler. In addition, if the addToFirebase()
function is a part of your class, you will likely need to use this.addToFirebase
instead.
2
@HemadriDasari They're null, because at the time thataddToFirebase()
is run, the actual DOM hasn't been rendered yet, so none of the elements with those ID's exist. After applying the onClick function correctly, when the button gets clicked, those elements should indeed exist in the DOM, and the error should go away.
– FrankerZ
Nov 23 '18 at 5:36
add a comment |
You have an error in your onClick
handler, it should be addToFirebase
not addToFirebase()
:
<Button
variant="contained"
color="secondary"
onClick={addToFirebase}
>
When you include ()
, it will run the addToFirebase
function, and use the return value as the onClick
handler. In addition, if the addToFirebase()
function is a part of your class, you will likely need to use this.addToFirebase
instead.
You have an error in your onClick
handler, it should be addToFirebase
not addToFirebase()
:
<Button
variant="contained"
color="secondary"
onClick={addToFirebase}
>
When you include ()
, it will run the addToFirebase
function, and use the return value as the onClick
handler. In addition, if the addToFirebase()
function is a part of your class, you will likely need to use this.addToFirebase
instead.
answered Nov 23 '18 at 5:18
FrankerZFrankerZ
17.8k73066
17.8k73066
2
@HemadriDasari They're null, because at the time thataddToFirebase()
is run, the actual DOM hasn't been rendered yet, so none of the elements with those ID's exist. After applying the onClick function correctly, when the button gets clicked, those elements should indeed exist in the DOM, and the error should go away.
– FrankerZ
Nov 23 '18 at 5:36
add a comment |
2
@HemadriDasari They're null, because at the time thataddToFirebase()
is run, the actual DOM hasn't been rendered yet, so none of the elements with those ID's exist. After applying the onClick function correctly, when the button gets clicked, those elements should indeed exist in the DOM, and the error should go away.
– FrankerZ
Nov 23 '18 at 5:36
2
2
@HemadriDasari They're null, because at the time that
addToFirebase()
is run, the actual DOM hasn't been rendered yet, so none of the elements with those ID's exist. After applying the onClick function correctly, when the button gets clicked, those elements should indeed exist in the DOM, and the error should go away.– FrankerZ
Nov 23 '18 at 5:36
@HemadriDasari They're null, because at the time that
addToFirebase()
is run, the actual DOM hasn't been rendered yet, so none of the elements with those ID's exist. After applying the onClick function correctly, when the button gets clicked, those elements should indeed exist in the DOM, and the error should go away.– FrankerZ
Nov 23 '18 at 5:36
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%2f53440954%2ftypeerror-cannot-read-property-value-of-null-reactjs%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
Pass function reference rather than call in onCick={addToFirebase}
– Diljohn5741
Nov 23 '18 at 5:18