首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Persona - onRenderPrimaryText -传递添加参数?

Persona - onRenderPrimaryText -传递添加参数?
EN

Stack Overflow用户
提问于 2019-12-21 06:26:15
回答 1查看 85关注 0票数 0

我正在遍历用户列表,并希望修改角色名称以超链接到特定页面。

代码语言:javascript
复制
          {this.props.users.map((u: any) => {

            //u has a property called u.ProfileUrl

            return (
              <div className={styles.personaTile}>
                <Persona
                  text={u.DisplayName}
                  size={PersonaSize.size48}
                  className={styles.persona}
                  imageInitials="true"
                  onRenderPrimaryText={this._onRenderPrimaryText}
                />
              </div>
            );
          })}

如何从uthis.props.user映射对象引用onRenderPrimaryText方法中的值?我不明白如何将它作为参数传递或从props引用它。我要使用props.componentRef

代码语言:javascript
复制
  private _onRenderPrimaryText = (props: IPersonaProps): JSX.Element => {
    return (
      <div className={styles.personaName}>
        <a href={  ???????  }>{props.text}</a>   // <-- How do I reference the u.ProfileUrl here?
      </div>
    );
    // tslint:disable-next-line:semicolon
  };
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-01-04 05:44:43

没有直接的方法来传递额外的数据,这样你就可以在你的自定义渲染器中使用它,但是通过一个小的JS闭包魔术,我想到了this working solution。如果你在你的项目和样式中关心这一点,那么你仍然需要处理的就是类型。从真正快速地看,这两种方法都是可行的。

代码语言:javascript
复制
const PersonaCustomRenderExample: React.FunctionComponent = () => {
  const personas = examplePersonas.map(examplePersona => {
    const customPrimaryTextRenderer = getCustomPrimaryTextRenderer(examplePersona);

    return (
      <Persona
        {...examplePersona}
        size={PersonaSize.size72}
        presence={PersonaPresence.offline}
        onRenderPrimaryText={customPrimaryTextRenderer}
        styles={{ root: { margin: "0 0 10px 0" } }}
        imageAlt="Annie Lindqvist, status is offline."
      />
    );
  });

  return <Stack tokens={{ childrenGap: 10 }}>{personas}</Stack>;
};

function getCustomPrimaryTextRenderer(persona) {
   return (props: IPersonaProps): JSX.Element => {
     return (
       <div>
         <a href={persona.profileUrl}>{props.secondaryText}</a>
       </div>
     );
  };
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59431950

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档