[英]Passing multiple props to React component
I am trying to pass two argument using props in ImageText
component.我正在尝试使用ImageText
组件中的道具传递两个参数。
I am not sure if it is right method or I have to create a map and then pass it.我不确定它是否是正确的方法,或者我必须创建一个 map 然后传递它。
import React, { PropTypes, Component } from 'react'
const ImageText = () => (
<div className="img-with-text">
<img className="img" src={props.imageUrl} />
<p className="txt">{props.imageText}</p>
</div>
);
export default ImageText;
Calling this component from another as follows从另一个调用这个组件如下
<ImageText imageUrl="/js.com" imageText="food"/>
But is throwing error as但是正在抛出错误
Uncaught (in promise) ReferenceError: props is not defined
at ImageText
In your case issue with you are using "Arrow functions" which needs to pass params inside brackets 在您的情况下,您正在使用“箭头功能”,需要在括号内传递参数
const ImageText = () => (
Should be 应该
const ImageText = (props) => (
Now 现在
let props = {
imageUrl:"/js.com",
imageText:""food""
}
<ImageText {...props} />
Access inside ImageText like 在ImageText里面访问就像
{props.imageUrl} or {props.imageText}
When you define your component like that, you need to pass your props as parameters to the anonymous function: 当您像这样定义组件时,需要将props作为参数传递给匿名函数:
const ImageText = ({imageUrl, imageText}) => ( ... rest of the code ... ); const ImageText =({imageUrl,imageText})=>(...代码的其余部分......);
When using a functional component (when you don't use a class) you must pass the props as an argument into to the function. 使用功能组件时(不使用类时),必须将props作为参数传递给函数。
You can pass as many props add you need into a component. 您可以将所需的道具添加到组件中。
const ImageText = (props) => (... const ImageText =(props)=>(...
If using a standard component (as a class) you would call a prop with 如果使用标准组件(作为类),您可以使用标准组件
this.props this.props
you are passing the props to dump component. 你传递道具转储组件。 it's not react component. 它不是反应成分。 pass the props to dump as function argument. 传递道具以转储为函数参数。
> import React, { PropTypes, Component } from 'react'
>
> const ImageText = ({imageUrl, imageText}) => (
> <div className="img-with-text">
> <img className="img" src={imageUrl} />
> <p className="txt">{imageText}</p>
> </div> );
>
> export default ImageText;
Passing down multiple props in reactJS can be done using either of following ways:在 reactJS 中传递多个道具可以使用以下任一方式完成:
// in App.js
<Modal post= {postProps} user={userDetails}/>
// in Modal.js
const Modal = (props) => {
const title = props.post.title
const username = props.user.username
// rest of the code..
}
in above example props (in Modal.js) forms an object with keys named post
and user
, thus you can access them like objects.在上面的示例道具中(在 Modal.js 中) forms 和 object 带有名为post
和user
的键,因此您可以像对象一样访问它们。
Another way would be:另一种方法是:
// in App.js
<Modal post={postProps}/>
// in Modal.js
const Modal = ({post}) => {
const title = post.title
// rest of the code..
}
in this example post
object has been imported explicitly in Modal.js在此示例中post
已在 Modal.js 中显式导入
Final Remark
: With first approach it gets very clear looking at your code that what has been passed down as props from parent element and might be clear to read. Final Remark
:使用第一种方法,可以非常清楚地查看您的代码,哪些是作为道具从父元素传递下来的,并且可能很容易阅读。
While with second approach i argue that, it is better to use in scenarios where its just one prop to pass, since you don't have to write props.
虽然我认为使用第二种方法,但最好在只有一个道具通过的情况下使用,因为您不必编写props.
everytime每次
Creating:创建:
const ImageText = ({ imageUrl, imageText }) => (
<div className="img-with-text">
<img className="img" src={imageUrl} />
<p className="txt">{imageText}</p>
</div>
);
export default ImageText;
Using;使用; when you have already defined imageUrl
and imageText
somewhere before the return
keyword:当您已经在return
关键字之前的某处定义imageUrl
和imageText
时:
<ImageText {...{imageUrl, imageText}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.