Why is my react function returning [object,object]
I have a function in my Numbers component that should return the state however it returns [object,object] I cant see what i've done wrong?
I've written a function in Numbers, that returns another function in my apps component!
import React, { Component } from 'react';
import Numbers from './Numbers'
import './App.css';
class App extends Component {
constructor(props){
super()
this.state={
calcValue:0
}
}
takeValue = (n) => {
alert(n)
}
render() {
return (
<div className="App">
<Numbers submit={(n) => this.takeValue(n)} numberValue={1}/>
</div>
);
}
}
export default App;
Number component:
import React, { Component } from 'react';
import propTypes from 'prop-types';
class Numbers extends Component {
constructor(props){
super();
this.state={
numberValue:6
}
}
submit = (n) => {
this.props.takeValue(this.state.numberValue)
}
render() {
return (
<div>
<button value={this.state.numberValue} onClick={this.props.submit}>
{this.props.numberValue}
</button>
</div>
)
}
}
// Completed.propTypes={
// test:propTypes.string.isRequired
export default Numbers
reactjs
add a comment |
I have a function in my Numbers component that should return the state however it returns [object,object] I cant see what i've done wrong?
I've written a function in Numbers, that returns another function in my apps component!
import React, { Component } from 'react';
import Numbers from './Numbers'
import './App.css';
class App extends Component {
constructor(props){
super()
this.state={
calcValue:0
}
}
takeValue = (n) => {
alert(n)
}
render() {
return (
<div className="App">
<Numbers submit={(n) => this.takeValue(n)} numberValue={1}/>
</div>
);
}
}
export default App;
Number component:
import React, { Component } from 'react';
import propTypes from 'prop-types';
class Numbers extends Component {
constructor(props){
super();
this.state={
numberValue:6
}
}
submit = (n) => {
this.props.takeValue(this.state.numberValue)
}
render() {
return (
<div>
<button value={this.state.numberValue} onClick={this.props.submit}>
{this.props.numberValue}
</button>
</div>
)
}
}
// Completed.propTypes={
// test:propTypes.string.isRequired
export default Numbers
reactjs
add a comment |
I have a function in my Numbers component that should return the state however it returns [object,object] I cant see what i've done wrong?
I've written a function in Numbers, that returns another function in my apps component!
import React, { Component } from 'react';
import Numbers from './Numbers'
import './App.css';
class App extends Component {
constructor(props){
super()
this.state={
calcValue:0
}
}
takeValue = (n) => {
alert(n)
}
render() {
return (
<div className="App">
<Numbers submit={(n) => this.takeValue(n)} numberValue={1}/>
</div>
);
}
}
export default App;
Number component:
import React, { Component } from 'react';
import propTypes from 'prop-types';
class Numbers extends Component {
constructor(props){
super();
this.state={
numberValue:6
}
}
submit = (n) => {
this.props.takeValue(this.state.numberValue)
}
render() {
return (
<div>
<button value={this.state.numberValue} onClick={this.props.submit}>
{this.props.numberValue}
</button>
</div>
)
}
}
// Completed.propTypes={
// test:propTypes.string.isRequired
export default Numbers
reactjs
I have a function in my Numbers component that should return the state however it returns [object,object] I cant see what i've done wrong?
I've written a function in Numbers, that returns another function in my apps component!
import React, { Component } from 'react';
import Numbers from './Numbers'
import './App.css';
class App extends Component {
constructor(props){
super()
this.state={
calcValue:0
}
}
takeValue = (n) => {
alert(n)
}
render() {
return (
<div className="App">
<Numbers submit={(n) => this.takeValue(n)} numberValue={1}/>
</div>
);
}
}
export default App;
Number component:
import React, { Component } from 'react';
import propTypes from 'prop-types';
class Numbers extends Component {
constructor(props){
super();
this.state={
numberValue:6
}
}
submit = (n) => {
this.props.takeValue(this.state.numberValue)
}
render() {
return (
<div>
<button value={this.state.numberValue} onClick={this.props.submit}>
{this.props.numberValue}
</button>
</div>
)
}
}
// Completed.propTypes={
// test:propTypes.string.isRequired
export default Numbers
reactjs
reactjs
edited Dec 31 '18 at 11:42
EatYaFood
537212
537212
asked Dec 31 '18 at 9:50
LeeLee
195
195
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You are not understanding concept of passing the props. you need to differentiate what you are passing into child component and how you want to call the parent from child.
Change
onClick={this.props.submit}
to
onClick={this.submit}
and
submit=(n) => {
this.props.takeValue(this.state.numberValue)
}
to
submit = (n) => {
this.props.submit(this.state.numberValue)
}
onClick={this.props.submit}
will call parent component not the child one
Demo
1
Thanks Yes I have seen my mistakes. I am new to React.
– Lee
Dec 31 '18 at 10:04
@Lee yes it happens :)
– Just code
Dec 31 '18 at 10:04
add a comment |
Thanks I have found my error! Should be this.submit
not this.props.submit
Also I needed to call the takeValue
function
Thanks
yep. I think this is what you're looking for: stackblitz.com/edit/react-dobewq
– Liren Yeo
Dec 31 '18 at 10:07
This section is reserved for answers, you can thank @Just code by accepting his answer
– Treycos
Dec 31 '18 at 12:42
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%2f53985994%2fwhy-is-my-react-function-returning-object-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You are not understanding concept of passing the props. you need to differentiate what you are passing into child component and how you want to call the parent from child.
Change
onClick={this.props.submit}
to
onClick={this.submit}
and
submit=(n) => {
this.props.takeValue(this.state.numberValue)
}
to
submit = (n) => {
this.props.submit(this.state.numberValue)
}
onClick={this.props.submit}
will call parent component not the child one
Demo
1
Thanks Yes I have seen my mistakes. I am new to React.
– Lee
Dec 31 '18 at 10:04
@Lee yes it happens :)
– Just code
Dec 31 '18 at 10:04
add a comment |
You are not understanding concept of passing the props. you need to differentiate what you are passing into child component and how you want to call the parent from child.
Change
onClick={this.props.submit}
to
onClick={this.submit}
and
submit=(n) => {
this.props.takeValue(this.state.numberValue)
}
to
submit = (n) => {
this.props.submit(this.state.numberValue)
}
onClick={this.props.submit}
will call parent component not the child one
Demo
1
Thanks Yes I have seen my mistakes. I am new to React.
– Lee
Dec 31 '18 at 10:04
@Lee yes it happens :)
– Just code
Dec 31 '18 at 10:04
add a comment |
You are not understanding concept of passing the props. you need to differentiate what you are passing into child component and how you want to call the parent from child.
Change
onClick={this.props.submit}
to
onClick={this.submit}
and
submit=(n) => {
this.props.takeValue(this.state.numberValue)
}
to
submit = (n) => {
this.props.submit(this.state.numberValue)
}
onClick={this.props.submit}
will call parent component not the child one
Demo
You are not understanding concept of passing the props. you need to differentiate what you are passing into child component and how you want to call the parent from child.
Change
onClick={this.props.submit}
to
onClick={this.submit}
and
submit=(n) => {
this.props.takeValue(this.state.numberValue)
}
to
submit = (n) => {
this.props.submit(this.state.numberValue)
}
onClick={this.props.submit}
will call parent component not the child one
Demo
answered Dec 31 '18 at 10:02
Just codeJust code
10.4k53066
10.4k53066
1
Thanks Yes I have seen my mistakes. I am new to React.
– Lee
Dec 31 '18 at 10:04
@Lee yes it happens :)
– Just code
Dec 31 '18 at 10:04
add a comment |
1
Thanks Yes I have seen my mistakes. I am new to React.
– Lee
Dec 31 '18 at 10:04
@Lee yes it happens :)
– Just code
Dec 31 '18 at 10:04
1
1
Thanks Yes I have seen my mistakes. I am new to React.
– Lee
Dec 31 '18 at 10:04
Thanks Yes I have seen my mistakes. I am new to React.
– Lee
Dec 31 '18 at 10:04
@Lee yes it happens :)
– Just code
Dec 31 '18 at 10:04
@Lee yes it happens :)
– Just code
Dec 31 '18 at 10:04
add a comment |
Thanks I have found my error! Should be this.submit
not this.props.submit
Also I needed to call the takeValue
function
Thanks
yep. I think this is what you're looking for: stackblitz.com/edit/react-dobewq
– Liren Yeo
Dec 31 '18 at 10:07
This section is reserved for answers, you can thank @Just code by accepting his answer
– Treycos
Dec 31 '18 at 12:42
add a comment |
Thanks I have found my error! Should be this.submit
not this.props.submit
Also I needed to call the takeValue
function
Thanks
yep. I think this is what you're looking for: stackblitz.com/edit/react-dobewq
– Liren Yeo
Dec 31 '18 at 10:07
This section is reserved for answers, you can thank @Just code by accepting his answer
– Treycos
Dec 31 '18 at 12:42
add a comment |
Thanks I have found my error! Should be this.submit
not this.props.submit
Also I needed to call the takeValue
function
Thanks
Thanks I have found my error! Should be this.submit
not this.props.submit
Also I needed to call the takeValue
function
Thanks
edited Dec 31 '18 at 13:15
Tony Bui
41412
41412
answered Dec 31 '18 at 10:03
LeeLee
195
195
yep. I think this is what you're looking for: stackblitz.com/edit/react-dobewq
– Liren Yeo
Dec 31 '18 at 10:07
This section is reserved for answers, you can thank @Just code by accepting his answer
– Treycos
Dec 31 '18 at 12:42
add a comment |
yep. I think this is what you're looking for: stackblitz.com/edit/react-dobewq
– Liren Yeo
Dec 31 '18 at 10:07
This section is reserved for answers, you can thank @Just code by accepting his answer
– Treycos
Dec 31 '18 at 12:42
yep. I think this is what you're looking for: stackblitz.com/edit/react-dobewq
– Liren Yeo
Dec 31 '18 at 10:07
yep. I think this is what you're looking for: stackblitz.com/edit/react-dobewq
– Liren Yeo
Dec 31 '18 at 10:07
This section is reserved for answers, you can thank @Just code by accepting his answer
– Treycos
Dec 31 '18 at 12:42
This section is reserved for answers, you can thank @Just code by accepting his answer
– Treycos
Dec 31 '18 at 12:42
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%2f53985994%2fwhy-is-my-react-function-returning-object-object%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