简体   繁体   English

如何为功能性React组件props实施解构分配?

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

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 ? 有关如何处理此问题的任何想法?

Your're passing prop from App as name not username 您正在通过App名称(而不是用户名)传递道具

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM