react ref value is empty
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I have a react component with an uncontrolled input element like below:
class TestComponent {
onAddTypeClicked = el => {
console.log(el);
console.log(el.value);
};
render() {
return (
<div>
<input
name="asset_types"
ref={el => this.assetTypesAdd = el}
/>
<button
type="button"
onClick={e => this.onAddTypeClicked(this.assetTypesAdd)}
/>
</div>
);
}
Now when I type some value in input field and click button, the input is getting printed correctly but it's value is empty. It works fine if I use document.getElementById approach though. Can anyone point if I'm doing anything wrong, please?
javascript reactjs
add a comment |
I have a react component with an uncontrolled input element like below:
class TestComponent {
onAddTypeClicked = el => {
console.log(el);
console.log(el.value);
};
render() {
return (
<div>
<input
name="asset_types"
ref={el => this.assetTypesAdd = el}
/>
<button
type="button"
onClick={e => this.onAddTypeClicked(this.assetTypesAdd)}
/>
</div>
);
}
Now when I type some value in input field and click button, the input is getting printed correctly but it's value is empty. It works fine if I use document.getElementById approach though. Can anyone point if I'm doing anything wrong, please?
javascript reactjs
Would you try to pass.current
? sothis.onAddTypeClicked(this.assetTypesAdd.current);
– Sung M. Kim
Nov 22 '18 at 4:40
current is valid if we create ref using React.createRef() but in my case I'm using callback pattern.
– geekprogrammer
Nov 22 '18 at 5:14
add a comment |
I have a react component with an uncontrolled input element like below:
class TestComponent {
onAddTypeClicked = el => {
console.log(el);
console.log(el.value);
};
render() {
return (
<div>
<input
name="asset_types"
ref={el => this.assetTypesAdd = el}
/>
<button
type="button"
onClick={e => this.onAddTypeClicked(this.assetTypesAdd)}
/>
</div>
);
}
Now when I type some value in input field and click button, the input is getting printed correctly but it's value is empty. It works fine if I use document.getElementById approach though. Can anyone point if I'm doing anything wrong, please?
javascript reactjs
I have a react component with an uncontrolled input element like below:
class TestComponent {
onAddTypeClicked = el => {
console.log(el);
console.log(el.value);
};
render() {
return (
<div>
<input
name="asset_types"
ref={el => this.assetTypesAdd = el}
/>
<button
type="button"
onClick={e => this.onAddTypeClicked(this.assetTypesAdd)}
/>
</div>
);
}
Now when I type some value in input field and click button, the input is getting printed correctly but it's value is empty. It works fine if I use document.getElementById approach though. Can anyone point if I'm doing anything wrong, please?
javascript reactjs
javascript reactjs
edited Nov 22 '18 at 4:55
Matthew Herbst
11.4k134890
11.4k134890
asked Nov 22 '18 at 4:31
geekprogrammergeekprogrammer
509525
509525
Would you try to pass.current
? sothis.onAddTypeClicked(this.assetTypesAdd.current);
– Sung M. Kim
Nov 22 '18 at 4:40
current is valid if we create ref using React.createRef() but in my case I'm using callback pattern.
– geekprogrammer
Nov 22 '18 at 5:14
add a comment |
Would you try to pass.current
? sothis.onAddTypeClicked(this.assetTypesAdd.current);
– Sung M. Kim
Nov 22 '18 at 4:40
current is valid if we create ref using React.createRef() but in my case I'm using callback pattern.
– geekprogrammer
Nov 22 '18 at 5:14
Would you try to pass
.current
? so this.onAddTypeClicked(this.assetTypesAdd.current);
– Sung M. Kim
Nov 22 '18 at 4:40
Would you try to pass
.current
? so this.onAddTypeClicked(this.assetTypesAdd.current);
– Sung M. Kim
Nov 22 '18 at 4:40
current is valid if we create ref using React.createRef() but in my case I'm using callback pattern.
– geekprogrammer
Nov 22 '18 at 5:14
current is valid if we create ref using React.createRef() but in my case I'm using callback pattern.
– geekprogrammer
Nov 22 '18 at 5:14
add a comment |
1 Answer
1
active
oldest
votes
I believe your code is working correctly and the value of accessible in this.assetTypesAdd.value
and its printed value is the same. I'm not sure where you are getting an empty value for the input.
// Example class component
class MyComponent extends React.Component {
onAddTypeClicked = el => {
console.log(el.value);
this.forceUpdate();
};
render() {
return (
<div>
<input
name="asset_types"
ref={el => this.assetTypesAdd = el}
/>
<button
type="button"
onClick={e => this.onAddTypeClicked(this.assetTypesAdd)}
>
Click me
</button>
{this.assetTypesAdd &&
<div>Value of input: {this.assetTypesAdd.value}</div>}
</div>
);
}
}
// Render it
ReactDOM.render(
<MyComponent/>,
document.getElementById("react")
);
button {
width: 100px;
height: 20px;
display: block;
margin: 10px 0;
}
<div id="react"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
Thanks for your answer. I just realized that there was a mistake in my code.
– geekprogrammer
Nov 22 '18 at 5:23
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%2f53423927%2freact-ref-value-is-empty%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
I believe your code is working correctly and the value of accessible in this.assetTypesAdd.value
and its printed value is the same. I'm not sure where you are getting an empty value for the input.
// Example class component
class MyComponent extends React.Component {
onAddTypeClicked = el => {
console.log(el.value);
this.forceUpdate();
};
render() {
return (
<div>
<input
name="asset_types"
ref={el => this.assetTypesAdd = el}
/>
<button
type="button"
onClick={e => this.onAddTypeClicked(this.assetTypesAdd)}
>
Click me
</button>
{this.assetTypesAdd &&
<div>Value of input: {this.assetTypesAdd.value}</div>}
</div>
);
}
}
// Render it
ReactDOM.render(
<MyComponent/>,
document.getElementById("react")
);
button {
width: 100px;
height: 20px;
display: block;
margin: 10px 0;
}
<div id="react"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
Thanks for your answer. I just realized that there was a mistake in my code.
– geekprogrammer
Nov 22 '18 at 5:23
add a comment |
I believe your code is working correctly and the value of accessible in this.assetTypesAdd.value
and its printed value is the same. I'm not sure where you are getting an empty value for the input.
// Example class component
class MyComponent extends React.Component {
onAddTypeClicked = el => {
console.log(el.value);
this.forceUpdate();
};
render() {
return (
<div>
<input
name="asset_types"
ref={el => this.assetTypesAdd = el}
/>
<button
type="button"
onClick={e => this.onAddTypeClicked(this.assetTypesAdd)}
>
Click me
</button>
{this.assetTypesAdd &&
<div>Value of input: {this.assetTypesAdd.value}</div>}
</div>
);
}
}
// Render it
ReactDOM.render(
<MyComponent/>,
document.getElementById("react")
);
button {
width: 100px;
height: 20px;
display: block;
margin: 10px 0;
}
<div id="react"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
Thanks for your answer. I just realized that there was a mistake in my code.
– geekprogrammer
Nov 22 '18 at 5:23
add a comment |
I believe your code is working correctly and the value of accessible in this.assetTypesAdd.value
and its printed value is the same. I'm not sure where you are getting an empty value for the input.
// Example class component
class MyComponent extends React.Component {
onAddTypeClicked = el => {
console.log(el.value);
this.forceUpdate();
};
render() {
return (
<div>
<input
name="asset_types"
ref={el => this.assetTypesAdd = el}
/>
<button
type="button"
onClick={e => this.onAddTypeClicked(this.assetTypesAdd)}
>
Click me
</button>
{this.assetTypesAdd &&
<div>Value of input: {this.assetTypesAdd.value}</div>}
</div>
);
}
}
// Render it
ReactDOM.render(
<MyComponent/>,
document.getElementById("react")
);
button {
width: 100px;
height: 20px;
display: block;
margin: 10px 0;
}
<div id="react"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
I believe your code is working correctly and the value of accessible in this.assetTypesAdd.value
and its printed value is the same. I'm not sure where you are getting an empty value for the input.
// Example class component
class MyComponent extends React.Component {
onAddTypeClicked = el => {
console.log(el.value);
this.forceUpdate();
};
render() {
return (
<div>
<input
name="asset_types"
ref={el => this.assetTypesAdd = el}
/>
<button
type="button"
onClick={e => this.onAddTypeClicked(this.assetTypesAdd)}
>
Click me
</button>
{this.assetTypesAdd &&
<div>Value of input: {this.assetTypesAdd.value}</div>}
</div>
);
}
}
// Render it
ReactDOM.render(
<MyComponent/>,
document.getElementById("react")
);
button {
width: 100px;
height: 20px;
display: block;
margin: 10px 0;
}
<div id="react"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
// Example class component
class MyComponent extends React.Component {
onAddTypeClicked = el => {
console.log(el.value);
this.forceUpdate();
};
render() {
return (
<div>
<input
name="asset_types"
ref={el => this.assetTypesAdd = el}
/>
<button
type="button"
onClick={e => this.onAddTypeClicked(this.assetTypesAdd)}
>
Click me
</button>
{this.assetTypesAdd &&
<div>Value of input: {this.assetTypesAdd.value}</div>}
</div>
);
}
}
// Render it
ReactDOM.render(
<MyComponent/>,
document.getElementById("react")
);
button {
width: 100px;
height: 20px;
display: block;
margin: 10px 0;
}
<div id="react"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
// Example class component
class MyComponent extends React.Component {
onAddTypeClicked = el => {
console.log(el.value);
this.forceUpdate();
};
render() {
return (
<div>
<input
name="asset_types"
ref={el => this.assetTypesAdd = el}
/>
<button
type="button"
onClick={e => this.onAddTypeClicked(this.assetTypesAdd)}
>
Click me
</button>
{this.assetTypesAdd &&
<div>Value of input: {this.assetTypesAdd.value}</div>}
</div>
);
}
}
// Render it
ReactDOM.render(
<MyComponent/>,
document.getElementById("react")
);
button {
width: 100px;
height: 20px;
display: block;
margin: 10px 0;
}
<div id="react"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
answered Nov 22 '18 at 5:13
Shawn AndrewsShawn Andrews
965719
965719
Thanks for your answer. I just realized that there was a mistake in my code.
– geekprogrammer
Nov 22 '18 at 5:23
add a comment |
Thanks for your answer. I just realized that there was a mistake in my code.
– geekprogrammer
Nov 22 '18 at 5:23
Thanks for your answer. I just realized that there was a mistake in my code.
– geekprogrammer
Nov 22 '18 at 5:23
Thanks for your answer. I just realized that there was a mistake in my code.
– geekprogrammer
Nov 22 '18 at 5:23
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%2f53423927%2freact-ref-value-is-empty%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
Would you try to pass
.current
? sothis.onAddTypeClicked(this.assetTypesAdd.current);
– Sung M. Kim
Nov 22 '18 at 4:40
current is valid if we create ref using React.createRef() but in my case I'm using callback pattern.
– geekprogrammer
Nov 22 '18 at 5:14