3

could you please tell me why I am getting this error:

Objects are not valid as a React child (found: object with keys {seo_val, text_val}). If you meant to render a collection of children, use an array instead.

I am trying to hit http request and try to make dropdown .

import React from "react";

class DropDown extends React.Component {
  constructor(props) {
    super(props);
    console.log(this.props);
  }

  render() {
    const makeDropDown = () => {
      console.log(this.data);
      return this.props.data.map(x => {
        return <option>{x}</option>;
      });
    };
    return (
      <div>
        <div>
          <select>{makeDropDown()}</select>;
        </div>
      </div>
    );
  }
}

export default DropDown;

Sandbox.

0

2 Answers 2

4

Full error message:

Objects are not valid as a React child (found: object with keys {seo_val, text_val}).

Error is very clear, you are trying to render an object in jsx that contains two keys:

seo_val: "..."
text_val: "..."

Write it like this, (render the value that you want):

return <option key={x.seo_val}>{x.text_val}</option>;

Working sandbox.

Sign up to request clarification or add additional context in comments.

1 Comment

This is the correct answer, you can't just render objects, you need to render one of the values.
-1

Looks like x is an object.

Try

import React from "react";

class DropDown extends React.Component {
  constructor(props) {
    super(props);
    console.log(this.props);
  }

  render() {

    return (
      <div>
        <div>
          <select>{
            this.props.data.map(x => {
              return (<option key={x}>echo</option>);
            })
          }</select>;
        </div>
      </div>
    );
  }
}

export default DropDown;

And you'll see it works. Replace echo with {x} throws the error you mentioned.

Comments

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.