5

I have a form in which I am asking the user to input field values for a couple of fields, storing the field values in an state and displaying the state values in a customised format.

So, I have a couple of input fields and a submit button:

<button onClick={this.handleSubmit}>Submit</button>
         {
           this.state.credentials &&
           //<Credentials value={this.state}/>
            <Credentials value={JSON.stringify(this.state, undefined, 2)} />
         }

The Credentials function convert the state of the component in JSON format:

const Credentials = ({value} ) => {
    return <pre>{formatState(value)}</pre>;
}

The formatState function will basically manipulate the state values and display them in the way I want:

function formatState(state) {
  console.log("hi")
  console.log(state);
    const output = state.groups.reduce((final, s)=> {
      console.log(output)
      const values = Object.keys(s).reduce((out, o)=> {
        out[o] = s[o].map(k => Object.values(k))
        return out;
        }, {})
        final =  {...final, ...values}
        return final;
      }, {})
      console.log(output) 
  }

The state looks like this:

{
  "groups": [
    {
      "typeA": [
        {
          "name": "abc"
        },
        {
          "number": "13,14"
        }
      ],
      "typeB": [
        {
          "country": "xyz"
        },
        {
          "date1": "2019-05-14"
        }
      ]
    }
  ]
}

But I want the output like this:

groups: {
"typeA": [[abc],[13,14]],
"typeB": [[2019-05-14],[xyz]]
}

SO, reduce function is used to convert the state into the following output. But I getting the error : "TypeError: Cannot read property 'reduce' of undefined"

Please can anybody tell me why this is happening.

1
  • 3
    Don't stringify the state, while passing value to Credentials Commented May 15, 2019 at 5:21

1 Answer 1

2

Error is here <Credentials value={JSON.stringify(this.state, undefined, 2)} />. JSON.stringify produces string representaion of some object (this.state in your case). Argument state of formatState has type of string. It seems that you want to have state arguemnt to be object. So you should do

<Credentials value={this.state} />
Sign up to request clarification or add additional context in comments.

5 Comments

It works! Thanks! Could you also tell me how to display the "final" on the screen? because I can see my output on the console but not on the screen
Your formatState(state) doen't return value. add return output to the end of formatState(state)
I added a return output statement after console.log() in formatState(state). But it gives me this error: "Uncaught Invariant Violation: Objects are not valid as a React child (found: object with keys {typeA, typeB}). If you meant to render a collection of children, use an array instead"
Here JSON.Stringify may help. Do return JSON.Stringify(output)
Please consider accepting answer if it looks ok to you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.