我如何使用React CreateElement方法将对象传递给UI

时间:2019-05-22 10:27:17

标签: reactjs typescript sharepoint spfx

我正在尝试使用spfx(使用react)框架为现代共享点创建自定义Web部件。我想在Web部件上显示从msgraph(“ https://graph.microsoft.com/v1.0/me/”)响应中获得的_UserInfo变量。

我试图像描述属性那样传递它:

React.createElement(MyProfile,{
 description: this.properties.description,
 userInfo: this._UserInfo
});

但失败

我该怎么做?

MyProfileWebPart.ts

      import * as React from 'react';
import * as ReactDom from 'react-dom';
import { Version } from '@microsoft/sp-core-library';
import { BaseClientSideWebPart } from '@microsoft/sp-webpart-base';
import {
  IPropertyPaneConfiguration,
  PropertyPaneTextField
} from '@microsoft/sp-property-pane';

import * as strings from 'MyProfileWebPartStrings';
import MyProfile from './components/MyProfile';
import { IMyProfileProps } from './components/IMyProfileProps';
import { MSGraphClient } from '@microsoft/sp-http';
import * as MicrosoftGraph from '@microsoft/microsoft-graph-types';
import { GraphError } from '@microsoft/microsoft-graph-client';

export interface IMyProfileWebPartProps {
  description: string;
  //userInfo: MicrosoftGraph.User;
}

export default class MyProfileWebPart extends BaseClientSideWebPart<IMyProfileWebPartProps> {
  accessToken:string = ""
  private _UserInfo: MicrosoftGraph.User;

  public render(): void {

  let url = "https://graph.microsoft.com/v1.0/me/";
  let request = new Request(url, {
      method: "GET",
      headers: new Headers({
          "Authorization": "Bearer " + this.accessToken
      })
  });

  fetch(request)
  .then((response) => {
      response.json().then((res) => {
        this._UserInfo = res;
      });

  })
  .catch((error) => {
      console.error(error);
  });

    const element: React.ReactElement<IMyProfileProps > = React.createElement(MyProfile,{description: this.properties.description});

    ReactDom.render(element, this.domElement);
  }

  protected onDispose(): void {
    ReactDom.unmountComponentAtNode(this.domElement);
  }

  protected get dataVersion(): Version {
    return Version.parse('1.0');
  }

  protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
    return {
      pages: [
        {
          header: {
            description: strings.PropertyPaneDescription
          },
          groups: [
            {
              groupName: strings.BasicGroupName,
              groupFields: [
                PropertyPaneTextField('description', {
                  label: strings.DescriptionFieldLabel
                })
              ]
            }
          ]
        }
      ]
    };
  }

}

MyProfile.tsx

import * as React from 'react';
import styles from './MyProfile.module.scss';
import { IMyProfileProps } from './IMyProfileProps';
import { escape } from '@microsoft/sp-lodash-subset';

export default class MyProfile extends React.Component<IMyProfileProps, {}> {
  public render(): React.ReactElement<IMyProfileProps> {
return (
  <div className={ styles.myProfile }>
    <div className={ styles.container }>
      <div className={ styles.row }>
        <div className={ styles.column }>
          <span className={ styles.title }>{}</span>

        </div>
      </div>
    </div>
  </div>
);
  }
}

IMyProfileProps.ts

import { User } from "@microsoft/microsoft-graph-types";

export interface IMyProfileProps {
  description: string;
  //userInfo: User;
}

0 个答案:

没有答案