简体   繁体   English

反应道具(JSX)

[英]React props(JSX)

I was implementing a basic rating component in React and somehow i did this我在 React 中实现了一个基本的评分组件,不知何故我做到了

const Rating = ( {text},{value}) => {
return (
   <div className='rating'>
    <span>
        <i className={value>=1?'fas fa-star':value>=0.5?'fas fa-star-half-alt':'far fa-star'}></i>
    </span>
    </div>
)}

instead of this而不是这个

const Rating = ( {text,value}) => {
return (
   <div className='rating'>
    <span>
        <i className={value>=1?'fas fa-star':value>=0.5?'fas fa-star-half-alt':'far fa-star'}></i>
    </span>
    </div>
)}

Can anyone tell me why the value is not processed in the first part?谁能告诉我为什么第一部分没有处理该值?

PS I am relatively new in the react/javascript field, so sorry if it's a very basic question. PS我在react/javascript领域相对较新,如果这是一个非常基本的问题,我很抱歉。

this means you are expecting 2 objects as arguments.这意味着您需要2 个对象作为参数。

( {text},{value} ) 

this means you are expecting a single object as argument.这意味着您期望单个对象作为参数。

({ text, value }) 

When you create a component, you are normally only expecting one object argument, which we normally call it props.当你创建一个组件时,你通常只需要一个对象参数,我们通常称之为 props。

const Component = (props) => {}

If you are expecting to use the component this way如果您希望以这种方式使用组件

<Component text="I am text" value="I am value" />

Then you can expect to use ({ text, value }) to restructure the props.然后你可以期望使用 ({ text, value }) 来重组道具。 Which is equivalent to这相当于

const Component = (props) => {
    const { text, value } = props
}

because this ({text},{value}) != ( {text,value}).因为这个({text},{value}) != ( {text,value}).

when you do this ({text,value}) , you destructuring props.当你这样做({text,value}) ,你会破坏道具。

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

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