简体   繁体   English

如何使用reactjs通过json数据在列表中显示用户的名字和姓氏的第一个字母而不是图像

[英]How to show first letters of the firstname and lastname of the user in the list instead of image through json data using reactjs

I'm new to reactjs, doing a small react app which should hold first letter of User's firstname and lastname using json data in reactjs.我是reactjs的新手,正在做一个小型react应用程序,它应该使用reactjs中的json数据保存用户名字和姓氏的第一个字母。 but i'm unable to do it... I getting functions for static data but not for dynamic.但我做不到……我得到的是静态数据的函数,但不是动态的。

json data: json数据:

[ { "id": 1, "firstname": "Avelino", "lastname": "Cajes", "position": ".Net Developer", }, [ { "id": 1, "firstname": "Avelino", "lastname": "Cajes", "position": ".Net Developer", },

{ "id": 2, "firstname": "Nilson", "lastname": "Audrey", "position": "C#", } { "id": 2, "firstname": "Nilson", "lastname": "Audrey", "position": "C#", }

] ]

Can anyone help me to do this problem?谁能帮我解决这个问题? Thanks in advance.提前致谢。 我要这样

I want in this way我要这样

Install react-avatar :安装react-avatar

npm install react-avatar

Then try adapting this code to suit your use case然后尝试修改此代码以适合您的用例

import React from "react";
import Avatar from 'react-avatar';

let data = [ 
 { "id": 1, "firstname": "Avelino", "lastname": "Cajes", "position": ".Net Developer"}, 
 { "id": 2, "firstname": "Nilson", "lastname": "Audrey", "position": "C#", }
]

class MyComponent extends React.Component{
   state = {data}
   render(){
      return(
         <div>
            { this.state.data.map(item=>(
                  <Avatar name={item.firstname+" "+item.lastname} maxInitials={2}/>
              ))
            }
         </div>
      )
   }
}

You can find out more on how to use react-avatar here您可以在此处找到有关如何使用react-avatar信息

I think this is what u asked for我想这就是你要求的

pass this to text element: firstName.charAt(0).toUpperCase() +""+lastName.charAt(0).toUpperCase()将此传递给文本元素: firstName.charAt(0).toUpperCase() +""+lastName.charAt(0).toUpperCase()

     import { LinearGradient } from 'expo-linear-gradient';
    
    <LinearGradient colors={['#4c669f', '#3b5998', '#192f6a']} style= 
 {styles.linearGradient}>
      <Text style={styles.buttonText}>
        
      </Text>
    </LinearGradient>
    
    // Later on in your styles..
    var styles = StyleSheet.create({
      linearGradient: {
        flex: 1,
        paddingLeft: 15,
        paddingRight: 15,
        borderRadius: 5
      },
      buttonText: {
        fontSize: 18,
        fontFamily: 'Gill Sans',
        textAlign: 'center',
        margin: 10,
        color: '#ffffff',
        backgroundColor: 'transparent',
      },
    });

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

相关问题 如何正确警告 reactjs 中的某些变量姓氏和名字 - How to properly alert certain variable lastname and firstname in reactjs 如何在 mui 数据表的同一列中显示名字和姓氏? - how do i display firstname and lastname in same column in mui data tables? 我如何在反应中将名字/姓氏更改为名字/姓氏? 问题是它来自后端 - how can i change firstname/lastname to firstName/lastName in react? the problem is that it comes from the backend 我可以部分显示我的内容(显示名字而不是名字和姓氏) - I can partially display my content (displaying firstName instead of firstName and lastName) 如何在React.js中通过OnSubmit()而不是OnChange()渲染图像 - How do I render an image through OnSubmit() instead on OnChange() in reactjs 如何显示元素名字和姓氏...来自 strore redux - how to display element firstname and lastname ... from strore redux React Select - 如何在选项而不是硬编码选项中显示/迭代来自 JSON 数据的数据? - React Select - How to show / iterate through data from JSON data in option instead of hardcoding options? 如何在 ReactJs 中显示列表 - How to show a list in ReactJs 通过ReactJS消费JSON数据 - Consume JSON data through ReactJS 如何使用后端在reactjs中的表中显示数据 - how to show data in table in reactjs using backend
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM