Simple two component app. My idea that App.js has a simple counter. When I mount Demo1.js, it should display a simple header with counter value.
When I try to mount Demo1.js I receive an error "Uncaught Error: Objects are not valid as a React child (found: object with keys {props}). If you meant to render a collection of children, use an array instead."
I tried using Profiler for React components at Chrome DevTools, but I got stuck on message "Loading React Element Tree..."
// App.js
import React, { useState } from 'react';
import Demo1 from './Demo1';
function App() {
const [counter, setCounter] = useState (0);
const [Demo1visibility, setDemo1visibility]=useState(false);
return (
<div>
<h1> Counter is {counter}</h1>
<div>
<button onClick={ previous => setCounter( count => count + 1)}> + </button>
<button onClick={ previous => setCounter( count => count - 1)}> - </button>
</div>
<div>
<button onClick={ previous => setDemo1visibility(true)}> show Demo1 component </button>
</div>
{Demo1visibility && <Demo1 props={counter}/>}
</div>
);
}
export default App;
// Demo1.js
import React from 'react';
function Demo1 (props) {
return (
<div>
<h1> This is Demo1 component </h1>
<h2> Props are {props} </h2>
</div>
);
}
export default Demo1;
I tried changing several times. Something wrong with my syntax?