Как я могу передать объект в пользовательский интерфейс с помощью метода React CreateElement - PullRequest
0 голосов
/ 22 мая 2019

Я пытаюсь создать пользовательскую веб-часть для современной sharepoint, используя spfx (используя реагировать).Я хочу отобразить в веб-части переменную _UserInfo, которую я получил из msgraph ("https://graph.microsoft.com/v1.0/me/") ответ.

Я попытался передать его как свойство description, например так:

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;
}
...