Why is my react function returning [object,object]












0















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









share|improve this question





























    0















    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









    share|improve this question



























      0












      0








      0








      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









      share|improve this question
















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 31 '18 at 11:42









      EatYaFood

      537212




      537212










      asked Dec 31 '18 at 9:50









      LeeLee

      195




      195
























          2 Answers
          2






          active

          oldest

          votes


















          1














          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






          share|improve this answer



















          • 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



















          0














          Thanks I have found my error! Should be this.submit not this.props.submit
          Also I needed to call the takeValue function



          Thanks






          share|improve this answer


























          • 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











          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%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









          1














          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






          share|improve this answer



















          • 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














          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






          share|improve this answer



















          • 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








          1







          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






          share|improve this answer













          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







          share|improve this answer












          share|improve this answer



          share|improve this answer










          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














          • 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













          0














          Thanks I have found my error! Should be this.submit not this.props.submit
          Also I needed to call the takeValue function



          Thanks






          share|improve this answer


























          • 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
















          0














          Thanks I have found my error! Should be this.submit not this.props.submit
          Also I needed to call the takeValue function



          Thanks






          share|improve this answer


























          • 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














          0












          0








          0







          Thanks I have found my error! Should be this.submit not this.props.submit
          Also I needed to call the takeValue function



          Thanks






          share|improve this answer















          Thanks I have found my error! Should be this.submit not this.props.submit
          Also I needed to call the takeValue function



          Thanks







          share|improve this answer














          share|improve this answer



          share|improve this answer








          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



















          • 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


















          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%2f53985994%2fwhy-is-my-react-function-returning-object-object%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







          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas