4

I make a react.js app using the API "https://www.definitions.net/definitions_api.php". When I log data.result, it outputs the right array of results, but when I map through the state, I get this error: "Objects are not valid as a React child (found: object with keys {}). If you meant to render a collection of children, use an array instead."

//states
  const [definition, setDefinition] = useState([])
  const [search, setSearch] = useState('')
  const [query, setQuery] = useState('')

  useEffect(() => {
    getDef()
   }, [query])


//FETCH
  const getDef = async () => {
  const response = await fetch(`....`)
  const data = await response.json()
  setDefinition(data.result);

//handle input
  const updateSearch = e => {
    setSearch(e.target.value)

  }

//get the final query onSubmit
const getQuery = e =>{
  e.preventDefault()
  setQuery(search)
  setSearch('')
}


 return (

  { definition && definition.map(item => (
       <Definition term={item.term} 
                  partofspeech = {item.partofspeech}
                  definition={item.definition} 
                  example = {item.example}                 
             />

            )
         )
        }

  );

in Definitions component I destructure the props:

function Definition({term, partofspeech, definition, example}) {
    return (
        <div className="definition">
            <h4>{term} ({partofspeech})</h4>
              <p>{definition}</p>
               <p>{example}</p>

        </div>
    )
}

Any help is welcome, thanks.

9
  • What is this ` <h4>{term} ({partofspeech})</h4>` ? partOfSpeech is it work ? Commented May 11, 2020 at 10:09
  • 1
    in one of the variable, you are trying to render an object. That is what React is telling you. Can you post the value of ` data.result`? Commented May 11, 2020 at 10:12
  • @KevinAmiranoff data.result logs: (33) 0: {term: "cat, true cat", definition: "feline mammal usually having thick soft fur and no ability to roar: domestic cats; wildcats", example: {…}, partofspeech: "noun"} 1: {term: "guy, cat, hombre, bozo", definition: "an informal term for a youth or man", example: ""a nice guy"; "the guy's only doing it for some doll"", partofspeech: "noun"} Commented May 11, 2020 at 13:11
  • data log: {result: Array(33)} result: Array(33) 0: {term: "cat, true cat", definition: "feline mammal usually having thick soft fur and no ability to roar: domestic cats; wildcats", example: {…}, partofspeech: "noun"} 1: {term: "guy, cat, hombre, bozo", definition: "an informal term for a youth or man", example: ""a nice guy"; "the guy's only doing it for some doll"", partofspeech: "noun"} Commented May 11, 2020 at 13:11
  • example: {…}, that could be the issue I think Commented May 11, 2020 at 13:12

2 Answers 2

3

Try this, when you loop the component loop the definition without the root element, it will cause error

const Component = () => {
  ....
  return (
    <Fragment>
      { definition && definition.map(item => (
       <Definition term={item.term} 
                  partofspeech = {item.partofspeech}
                  definition={item.definition} 
                  example = {item.example}                 
             />

            )
         )
        }

  )
    </Fragment>
  )
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

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

Comments

0

in return you have both ({}) round and curly, you can remove both and you should return directly

return definition && definition.map(item => (
       <Definition term={item.term} 
            partofspeech = {item.partofspeech}
            definition={item.definition} 
            example = {item.example}                 
        />
    ));

Short snippet, you can run it and review :

const { useState } = React;

const App = () => {

  const [users,setUsers] = useState(['Vivek' , 'Darshita', 'Darshvi' , 'Coming..']);

  return users.map(user => <p>{user}</p>);
}

ReactDOM.render(<App />, document.getElementById('react-root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.4/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.4/umd/react-dom.production.min.js"></script>
<div id="react-root"></div>

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.