[英]How to find object in a List of Items by index using es6 (reactjs)
I'm new to react and es6 syntax, suppose I have a list of objects like below ( testData.js ) 我是React和es6语法的新手,假设我有下面的对象列表( testData.js )
export const favourites=[ { name: 'Jhon Doe', avatarUrl:require('../../social_app/test/profilePic/17.jpg') }, { name: 'Micheal Douglas', avatarUrl:require('../../social_app/test/profilePic/17.jpg') }, { name: 'Camille la cruz', avatarUrl:require('../../social_app/test/profilePic/17.jpg') }, { name: 'The Rock', avatarUrl:require('../../social_app/test/profilePic/17.jpg') }, { name: 'Daniel lacharmante', avatarUrl:require('../../social_app/test/profilePic/17.jpg') }, { name: 'Alber Weiter', avatarUrl:require('../../social_app/test/profilePic/17.jpg') } ]
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
And let say I've imported it into a file, let say test.js , so to use this list I've done 假设我已将其导入到文件中,例如test.js ,因此要使用此列表,我已经完成了
const favouritePersonList =favouritePersonData.map(person=>{ return <Avatar key={person} avatarurl={person.avatarUrl} style={styles.favouritePersons} height={40} width={40} activity/> })
which is suppose to create a List of Avatar component, and put the needed props etc...Now what I would like to do is to render the first object/component in this list. 假设要创建一个Avatar组件列表,并放置所需的道具等...现在我想做的是在此列表中渲染第一个对象/组件。 I tried
我试过了
render( <div> {favouritePersonList[0]} </div> )
But no success, can someone help me ? 但是没有成功,有人可以帮助我吗? thanks
谢谢
As requested please find the full avatar code, kindly note that it is a react native code. 根据要求,请找到完整的头像代码,请注意,这是一个响应本机代码。
export default class Avatar extends React.Component { constructor(props){ super(props) this.state={ userState : '#1cdb4c' } } componentWillMount(){ this.props.activity? this.setState({userState:'#1cdb4c'}): this.setState({userState:'#f93434'}) } render() { const {height, width, avatarUrl}=this.props return ( <View style={styles.container}> <View style={[styles.avatarContainer,{height:height,width:width}]}> <Image style={styles.avatarImg} source={avatarUrl} /> </View> <View style={[styles.userActivity,{height:height, width:width}]}> <View style={styles.outer}> <View style={[styles.inner,{backgroundColor:this.state.userState}]}/> </View> </View> </View> ); } }
It depends on how you want your component to display, but you might use something roughly like this: 这取决于您希望组件的显示方式,但是您可能会使用大致如下所示的内容:
render(
<div>
{favoritePersonList.map(person => {
return (
<div>
<h1>{person.name}</h1>
<img src={person.avatarUrl} alt={person.name}/>
</div>
)
})}
</div>
)
simply use this favouritePersonList
instead of this favouritePersonList[0]
because you want to render a whole list not just a single element of it so your code will be 只需使用此
favouritePersonList
而不是此favouritePersonList[0]
因为您要呈现整个列表而不只是其中的单个元素,因此您的代码将是
render() {
return (
<div>
{favouritePersonList}
</div>
)
}
Edit: 编辑:
I didn't notice you wanted to render the first element only 我没注意到您只想渲染第一个元素
The easiest way will be not to use map in the first place 最简单的方法是首先不使用地图
render() {
return (
<div>
{<Avatar avatarurl={favouritePersonData[0].avatarUrl} style={styles.favouritePersons} height={40} width={40} activity/>}
</div>
)
}
Think of it like you're just calling JavaScript functions. 就像您只是在调用JavaScript函数一样。 A for loop would work for you like below:
for循环适用于您,如下所示:
let avatarRows = [];
for (var i = 0; i < favourites.length; i++) {
// note: we add a key prop here to allow react to uniquely identify each
// element in this array. see: https://reactjs.org/docs/lists-and-keys.html
avatarRows.push(<Avatar key={favourites[i]} avatarurl={favourites[i].avatarUrl} style={styles.favouritePersons} height={40} width={40} activity/>);
}
return <div>{avatarRows}</div>;
Or 要么
return <div>{avatarRows[0]}</div>;
for the first Avatar component. 用于第一个头像组件。
render(
<div>
{favouritePersonList[0]}
</div>
)
The code above will try to render an object inside the div
which is wrong. 上面的代码将尝试在
div
内渲染对象 ,这是错误的。
So, the above code will basically look like this: 因此,以上代码基本上如下所示:
render(
<div>
{
name: 'Jhon Doe',
avatarUrl:require('../../social_app/test/profilePic/17.jpg')
}
</div>
)
You must do something like 你必须做类似的事情
render(
<div>
<Avatar key={favouritePersonList[0]} avatarurl={favouritePersonList[0].avatarUrl} style={styles.favouritePersons} height={40} width={40} activity/>
</div>
)
As I said in the comments, your code works for a simple case. 正如我在评论中所说,您的代码适用于简单情况。 Here it is:
这里是:
const favourites = [ { name: "Jhon Doe", }, { name: "Micheal Douglas", }, { name: "Camille la cruz", }, { name: "The Rock", }, { name: "Daniel lacharmante", }, { name: "Alber Weiter", }, ]; const Avatar = props => <div>{props.name}</div>; const App = () => { const favouritePersonList = favourites.map( person => ( <Avatar key={person.name} name={person.name} /> ) ); return <div>{favouritePersonList[ 0 ]}</div>; }; ReactDOM.render( <App />, document.getElementById( "root" ) );
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <div id="root"></div>
So, the problem must be somewhere else. 因此,问题一定在其他地方。 Please share us your whole code with the
Avatar
component. 请与
Avatar
组件分享我们的完整代码。
Update 更新
This is a blind suggestion but in your Avatar
component, you are using source={avatarUrl}
for the Image
. 这是一个盲目的建议,但在您的
Avatar
组件中,您正在使用source={avatarUrl}
作为Image
。 Two problems here. 这里有两个问题。
this.props
. So, just change the destructuring name. this.props
进行销毁。因此,只需更改销毁名称即可。 avatarurl
since you are passing it like that in the parent component. avatarurl
因为您像在父组件中那样传递它。 So, try like this: 因此,尝试这样:
export default class Avatar extends React.Component {
constructor(props){
super(props)
this.state={
userState : '#1cdb4c'
}
}
componentWillMount(){
this.props.activity? this.setState({userState:'#1cdb4c'}): this.setState({userState:'#f93434'})
}
render() {
const {height, width, avatarurl}=this.props
return (
<View style={styles.container}>
<View style={[styles.avatarContainer,{height:height,width:width}]}>
<Image style={styles.avatarImg}
source={avatarurl}
/>
</View>
<View style={[styles.userActivity,{height:height, width:width}]}>
<View style={styles.outer}>
<View style={[styles.inner,{backgroundColor:this.state.userState}]}/>
</View>
</View>
</View>
);
}
}
let favouritePersonList = [] favouritePersonData.foreach((person,index)=>{ favouritePersonList[index] = <Avatar key={person} avatarurl={person.avatarUrl} style={styles.favouritePersons} height={40} width={40} activity/> })
I think this will work for you, but i cant give you a solid reason why your methid didnt work, i would have to build an example and test it a bit 我认为这对您有用,但是我不能给您坚实的理由说明您的方法无法奏效,我将不得不建立一个示例并对其进行一些测试
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.