How can I implement destructuring assignment for functional react components props?

Salar Bahador

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 ?

Code Maniac

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')
);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I implement Redux to functional components that already have props?

How can I prevent re render of react functional components if no props changed

How i can destructuring {this.props.children}?

React: How can I acces to props of a functional component in a class component

Must use destructuring props assignment (react/destructuring-assignment)

How to implement global variable in react functional components

How can I access initialParams on functional components React Navigation

Can the props in a destructuring assignment be transformed in place?

How can i give a url to a react components using props?

How to specify (optional) default props with TypeScript for stateless, functional React components?

How can I use this in the left side of the object destructuring assignment?

how to use destructuring in react components

React: Passing down props to functional components

Getting undefined props in functional react components

Is there a way I can restructure part of a destructuring assignment?

Typescript ESLint Error - How can I type children when using react functional components?

Must use destructuring props assignment

How can I use {...this.props} in a functional component

How can I write a Screenplay Task to test React components which pass keyboard state as props

How can I use multiple props in a single callback defined in a template literal in React styled-components

React functional component destructuring

How can I refresh React Functional component?

How do I use React refs in a map for functional components?

How do I use setNativeProps in Functional Components? React-native

How can I expose functions to the ref using functional components?

Destructuring state/props in React

React - TypeScript destructuring of props

how to implement child routing in functional components

React - How to pass props down for the .map function when using functional components