3

I'm trying to pass functional component props by destructuring assignment method.

In this example below I have tried to use this concept but it doesn't work. the result of this code return empty and doesn't print that prop.

import React from 'react';
import { render } from 'react-dom';

const App = ({ username: name }) => (<h1>{username}</h1>)

render(
   <App name="Tom" />,
   document.getElementById('root')
);

Any ideas on how to handle this issue ?

1 Answer 1

7

Your're passing prop from App as name not username

change this

const App = ({ username : name })

to this

const App = ({ name: username })

import React from 'react';
import { render } from 'react-dom';

const App = ({ name: username }) => (<h1>{username}</h1>)

render(
   <App name="Tom" />,
   document.getElementById('root')
);
Sign up to request clarification or add additional context in comments.

4 Comments

Tnx it's working now. why is it filled the name and username values in some kind of opossit syntax?
@SalarBahador the syntax is { propertyName : reference variable } you can read this for more info
So this structure is different than other object structures in javascript? in the usual way, we say define object like : obj = { key : value }
@SalarBahador here you're not defining a object you're picking values from object

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.