Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Function with argument won't display

Goal:
Enable to use function Test2 with argument value "Test 2" and then to be displayed without any error.

Problem:
When I apply the code "<Test2 thename={‘Test 2’} />", it shows an error saying

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

What part of the code am I missing?

Stackblitz:
https://stackblitz.com/edit/react-ts-atrrsi?

Info:
*Newbie in ReactTS

Thank you!


import React, { Component } from 'react';
import { render } from 'react-dom';
import Hello from './Hello';
import './style.css';

interface AppProps {}
interface AppState {
  name: string;
}

export default function App() {
  function Test1() {
    return <h1>Test 1 works!</h1>;
  }

  function Test2(thename: string) {
    return <h1>{thename} works!</h1>;
  }

  return (
    <div>
      <Hello name={'sdf'} />
      <Test1 />
      <p>Start editing to see some magic happen :)</p>
    </div>
  );
}

render(<App />, document.getElementById('root'));

import React from 'react';

export default ({ name }) => <h1>Hello {name}!</h1>;

>Solution :

The props you pass to the react component are passed in an object. You need to access them from the object as follow.

function Test2(props) {
    return <h1>{props.thename} works!</h1>;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading