Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

Objects are not valid as a React child. If you meant to render a collection of children, use an array instead

return (
    <div className="col">
      <h1>Mi Casa</h1>
      <p>This is my house y&apos;all!</p>
      {homes.map(home => <div>{home.name}</div>)}
    </div>
  );
Comment

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

implemented a counter with increment, decrement and reset buttons and I was 
trying to display the count on to the screen but I was the above mentioned 
error.

In the code I had declared the count which is returned by useReducer hook as 
the object and I was directly trying to return it rather than the count 
property of it

I should actually return count.count and I was returning count only (object 
itself) not property.

We can stringify object and return the string also.

import React, { useReducer } from "react";

const initialState = {
count:0,
}
const reducer = (state, action) => {
    switch (action.type) {
        case 'increment':
            return {count:state.count + 1}
        case 'decrement':
            return {count:state.count - 1}
        case 'reset':
            return initialState
        default:
            return state
    }
}

function CounterWithReducer(props) {
  const [count, dispatch] = useReducer(reducer, initialState);

  return (
    <>
      <h1>{count}</h1>
      <button onClick={()=>{dispatch({type:'increment'})}}>Increment</button>

      <button onClick={()=>{dispatch({type:"decrement"})}}>Decrement</button>
      <button onClick={()=>{dispatch({type:"reset"})}}>Reset</button>
    </>
  );
}

export default CounterWithReducer;
In the above code {count} This section (in the return part ) is where I did the
mistake instead of count I need to use count.count

Summary is that if you trying to show object on to screen you can't either use 
JSON.stringify() or try to display any property of the object.
Comment

PREVIOUS NEXT
Code Example
Typescript :: how to check string array is sorted or not in typescript 
Typescript :: typescript -g doesnst read tsconfog 
Typescript :: who indirectly elects the president 
Typescript :: class-transformer change property names 
Typescript :: no audio endpoints registered 
Typescript :: does i5 7th generation processor supports windows 11 
Typescript :: Exclude code from hints delphi 7 
Typescript :: knex could not determine data type of parameter $1 
Typescript :: function that takes first & last name and then it greets the user using his full name. 
Typescript :: famous scientists who contributed to electricity 
Typescript :: how to loop through a specific number of elements in a list python 
Typescript :: CREATE FUNCTION which accepts LIST as argument 
Typescript :: how to check whether url is responding or not in typescript 
Typescript :: how many straight and curves are there in a standard track 
Typescript :: see all github issue comments i made site:stackoverflow.com 
Typescript :: scale a vector 
Typescript :: Basic structure of named imports and exports 
Typescript :: how to install tsu 
Typescript :: The Apple I had a built-in video terminal, sockets for __________ kilobytes of onboard random access memory (RAM), a keyboard, and a cassette board meant to work with regular cassette recorders. 
Typescript :: how to find muti column name exists in sql server 
Typescript :: vscode pass argument to registerCommand 
Typescript :: Local Variable in Jenkins 
Typescript :: typescript initialize stripe api, connect stripe with OAuth and creating Direct Charges in Stripe. 
Typescript :: how to execute the same test case for multiple time using testng? 
Typescript :: return type depends on input typescript 
Typescript :: React/Typescript Storybook not allowing objects to be imported 
Typescript :: github actions typescript 
Typescript :: w to check whether an image is a broken image or not in typescript angular 
Typescript :: como agregarle un rango a mat-datapicker 
Typescript :: typescript enum includes value 
ADD CONTENT
Topic
Content
Source link
Name
5+2 =