Overview

This solution contains two web parts where we are passing the data between them by changing the data into one web part that will reflect the change into the other web part. They are the connected web parts.

Implementation

  • Create the directory with the name ‘Connected Web parts’.
  • Create the solution by running Yeoxman generator using the below command.

    Pass Data Between Web Parts (Connected Web Parts) Using SPFx

  • The generator will ask the below information about the new SPFx solution.

    Pass Data Between Web Parts (Connected Web Parts) Using SPFx

  • Yeoman generator will scaffold the process to add the node packages to the existing SPFx solution. Once it is added to the solution, it will be shown with the below message.

    Pass Data Between Web Parts (Connected Web Parts) Using SPFx

  • Create another web part, which will receive the data from the Source connected web part.
  • To create the new web part into the existing solution, run the below command.

    Pass Data Between Web Parts (Connected Web Parts) Using SPFx

  • It will ask which type of component to create. We will use the ‘WebPart’ component in our case as shown below with providing the below information about the new web part.

    Pass Data Between Web Parts (Connected Web Parts) Using SPFx

  • Once the web part is created, open the solution code in the code editor (like Visual Studio Code) using the below command.

    Pass Data Between Web Parts (Connected Web Parts) Using SPFx

NPM Packages

Run the below command to add the packages into the solution.
  1. npm install rx-lite --save
  2. npm install @types/rx-lite --save

Update the Solution

Create the separate state files (‘IDataSenderWpState.ts’ and ‘IDataReceiverWpState.ts’) for both the web parts.
Create the new folder ‘RxJsEventEmitter’ inside the ‘webparts’ folder and create the ‘IEventData.ts’ and ‘RxJsEventEmitter.ts’ in the ‘RxJsEventEmitter’ folder.
The code structure for the current solution will be as below,
Pass Data Between Web Parts (Connected Web Parts) Using SPFx
Open the ‘IDataSenderWpState.ts’ and define the state object as shown below.
  1. export interface IDataSenderWpState {
  2. userName: string;
  3. password:string;
  4. }
Open the ‘IDataReceiverWpState.ts’ and define the state object as shown below.
  1. export interface IDataReceiverWpState {
  2. userName: string;
  3. password:string;
  4. }
Open ‘IEventData.ts’ and copy the below code.
  1. export interface IEventData {
  2. sharedUserName: string;
  3. sharedUserPassword:string;
  4. }
  5. export default IEventData;
Open ‘RxJsEventEmitter.ts’ and copy the below code.
  1. import { Subject } from "rx-lite";
  2. import IEventData from "./IEventData";
  3. export class RxJsEventEmitter {
  4. public subjects: Object;
  5. private constructor() {
  6. this.subjects = {};
  7. }
  8. public static getInstance(): RxJsEventEmitter
  9. {
  10. if (!window["RxJsEventEmitter"]) {
  11. window["RxJsEventEmitter"] = new RxJsEventEmitter();
  12. }
  13. return window["RxJsEventEmitter"];
  14. }
  15. public emit(name: string, data: IEventData): void
  16. {
  17. if (!this.subjects[name]) {
  18. this.subjects[name] = new Subject();
  19. }
  20. this.subjects[name].onNext(data);
  21. }
  22. public on(name: string, handler: any): void
  23. {
  24. if (!this.subjects[name]) {
  25. this.subjects[name] = new Subject();
  26. }
  27. this.subjects[name].subscribe(handler);
  28. }
  29. }
Open the 'DataSenderWp.tsx' file and add the below imports.
  1. import { IDataSenderWpState } from './IDataSenderWpState';
  2. import IEventData from '../../RxJsEventEmitter/IEventData';
  3. import { RxJsEventEmitter } from '../../RxJsEventEmitter/RxJsEventEmitter';
Update the render method and add the other functions. The code for the 'DataSenderWp.tsx' is as below. On change of the UserName or password text field value update, it will pass the data into the other web part using the 'SendData' method. Also, define the object of 'RxJsEventEmitter' inside the class.
  1. import * as React from 'react';
  2. import styles from './DataSenderWp.module.scss';
  3. import { IDataSenderWpProps } from './IDataSenderWpProps';
  4. import { escape } from '@microsoft/sp-lodash-subset';
  5. import { IDataSenderWpState } from './IDataSenderWpState';
  6. import IEventData from '../../RxJsEventEmitter/IEventData';
  7. import { RxJsEventEmitter } from '../../RxJsEventEmitter/RxJsEventEmitter';
  8. export default class DataSenderWp extends React.Component<IDataSenderWpProps, IDataSenderWpState> {
  9. private readonly eventEmitter: RxJsEventEmitter = RxJsEventEmitter.getInstance();
  10. public constructor(props:IDataSenderWpProps, state:IDataSenderWpState){
  11. super(props);
  12. this.state = {
  13. userName: "",
  14. password : ""
  15. };
  16. }
  17. public render(): React.ReactElement<IDataSenderWpProps> {
  18. return (
  19. <div className={styles.dataSenderWp}>
  20. <h2>Sender Web Part</h2>
  21. <div>User Name:</div>
  22. <div>
  23. <input type="text" value={this.state.userName} onChange={this._onChangeUserName.bind(this)} />
  24. </div>
  25. <div>Password:</div>
  26. <div>
  27. <input type="text" value={this.state.password} onChange={this._onChangePassword.bind(this)} />
  28. </div>
  29. </div>
  30. );
  31. }
  32. private _onChangeUserName(event: any)
  33. {
  34. this.setState({
  35. userName : event.target.value
  36. });
  37. this.sendData(event.target.value, this.state.password);
  38. }
  39. private _onChangePassword(event: any)
  40. {
  41. this.setState({
  42. password : event.target.value
  43. });
  44. this.sendData(this.state.userName, event.target.value);
  45. }
  46. private sendData(userName:string, password:string): void
  47. {
  48. var eventBody = {
  49. sharedUserName: userName,
  50. sharedUserPassword:password
  51. } as IEventData;
  52. this.eventEmitter.emit("shareData", eventBody);
  53. }
  54. }
Update 'DataSenderWp.module.scss' file as below.
  1. @import '~office-ui-fabric-react/dist/sass/References.scss';
  2. .dataSenderWp {
  3. .container {
  4. max-width: 700px;
  5. margin: 0px auto;
  6. box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.2), 0 25px 50px 0 rgba(0, 0, 0, 0.1);
  7. }
  8. h2{
  9. background-color: blue;
  10. color:white;
  11. padding: 10px 5px;
  12. }
  13. .row {
  14. @include ms-Grid-row;
  15. @include ms-fontColor-white;
  16. background-color: $ms-color-themeDark;
  17. padding: 20px;
  18. }
  19. .column {
  20. @include ms-Grid-col;
  21. @include ms-lg10;
  22. @include ms-xl8;
  23. @include ms-xlPush2;
  24. @include ms-lgPush1;
  25. }
  26. .title {
  27. @include ms-font-xl;
  28. @include ms-fontColor-white;
  29. }
  30. .subTitle {
  31. @include ms-font-l;
  32. @include ms-fontColor-white;
  33. }
  34. .description {
  35. @include ms-font-l;
  36. @include ms-fontColor-white;
  37. }
  38. .button {
  39. // Our button
  40. text-decoration: none;
  41. height: 32px;
  42. // Primary Button
  43. min-width: 80px;
  44. background-color: $ms-color-themePrimary;
  45. border-color: $ms-color-themePrimary;
  46. color: $ms-color-white;
  47. // Basic Button
  48. outline: transparent;
  49. position: relative;
  50. font-family: "Segoe UI WestEuropean","Segoe UI",-apple-system,BlinkMacSystemFont,Roboto,"Helvetica Neue",sans-serif;
  51. -webkit-font-smoothing: antialiased;
  52. font-size: $ms-font-size-m;
  53. font-weight: $ms-font-weight-regular;
  54. border-width: 0;
  55. text-align: center;
  56. cursor: pointer;
  57. display: inline-block;
  58. padding: 0 16px;
  59. .label {
  60. font-weight: $ms-font-weight-semibold;
  61. font-size: $ms-font-size-m;
  62. height: 32px;
  63. line-height: 32px;
  64. margin: 0 4px;
  65. vertical-align: top;
  66. display: inline-block;
  67. }
  68. }
  69. }
Open the 'DataReceiverWp.tsx' file and add the below imports.
  1. import { IDataReceiverWpState } from './IDataReceiverWpState';
  2. import IEventData from '../../RxJsEventEmitter/IEventData';
  3. import {RxJsEventEmitter} from '../../RxJsEventEmitter/RxJsEventEmitter';
Update the Render method and add the constructor which calls the function 'receiveData' to get the data from the Sender web part. The entire code for this file is as below.
  1. import * as React from 'react';
  2. import styles from './DataReceiverWp.module.scss';
  3. import { IDataReceiverWpProps } from './IDataReceiverWpProps';
  4. import { escape } from '@microsoft/sp-lodash-subset';
  5. import { IDataReceiverWpState } from './IDataReceiverWpState';
  6. import IEventData from '../../RxJsEventEmitter/IEventData';
  7. import {RxJsEventEmitter} from '../../RxJsEventEmitter/RxJsEventEmitter';
  8. export default class DataReceiverWp extends React.Component<IDataReceiverWpProps, IDataReceiverWpState>
  9. {
  10. private readonly eventEmitter: RxJsEventEmitter = RxJsEventEmitter.getInstance();
  11. public constructor(props:IDataReceiverWpProps, state:IDataReceiverWpState){
  12. super(props);
  13. this.state = {
  14. userName:"",
  15. password:""
  16. };
  17. this.eventEmitter.on("shareData", this.receiveData.bind(this));
  18. }
  19. public render(): React.ReactElement<IDataReceiverWpProps> {
  20. return (
  21. <div className={styles.dataReceiverWp}>
  22. <h2>Receiver web part</h2>
  23. <div><span>User Name: </span><span>{this.state.userName}</span></div>
  24. <div><span>Password: </span><span>{this.state.password}</span></div>
  25. </div>
  26. );
  27. }
  28. private receiveData(data: IEventData) {
  29. this.setState({
  30. userName: data.sharedUserName,
  31. password:data.sharedUserPassword
  32. });
  33. }
  34. }
Update 'DataSenderWp.module.scss' file as below.
  1. @import '~office-ui-fabric-react/dist/sass/References.scss';
  2. .dataReceiverWp
  3. {
  4. h2{
  5. background-color: blue;
  6. color:white;
  7. padding: 10px 5px;
  8. }
  9. .container {
  10. max-width: 700px;
  11. margin: 0px auto;
  12. box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.2), 0 25px 50px 0 rgba(0, 0, 0, 0.1);
  13. }
  14. .row {
  15. @include ms-Grid-row;
  16. @include ms-fontColor-white;
  17. background-color: $ms-color-themeDark;
  18. padding: 20px;
  19. }
  20. .column {
  21. @include ms-Grid-col;
  22. @include ms-lg10;
  23. @include ms-xl8;
  24. @include ms-xlPush2;
  25. @include ms-lgPush1;
  26. }
  27. .title {
  28. @include ms-font-xl;
  29. @include ms-fontColor-white;
  30. }
  31. .subTitle {
  32. @include ms-font-l;
  33. @include ms-fontColor-white;
  34. }
  35. .description {
  36. @include ms-font-l;
  37. @include ms-fontColor-white;
  38. }
  39. .button {
  40. // Our button
  41. text-decoration: none;
  42. height: 32px;
  43. // Primary Button
  44. min-width: 80px;
  45. background-color: $ms-color-themePrimary;
  46. border-color: $ms-color-themePrimary;
  47. color: $ms-color-white;
  48. // Basic Button
  49. outline: transparent;
  50. position: relative;
  51. font-family: "Segoe UI WestEuropean","Segoe UI",-apple-system,BlinkMacSystemFont,Roboto,"Helvetica Neue",sans-serif;
  52. -webkit-font-smoothing: antialiased;
  53. font-size: $ms-font-size-m;
  54. font-weight: $ms-font-weight-regular;
  55. border-width: 0;
  56. text-align: center;
  57. cursor: pointer;
  58. display: inline-block;
  59. padding: 0 16px;
  60. .label {
  61. font-weight: $ms-font-weight-semibold;
  62. font-size: $ms-font-size-m;
  63. height: 32px;
  64. line-height: 32px;
  65. margin: 0 4px;
  66. vertical-align: top;
  67. display: inline-block;
  68. }
  69. }
  70. }
Once all the changes in the file have been done, run the 'gulp serve' command to see the output into the Workbench.aspx page. It will have the below output for updating into the sender web part for the 'username' or 'password' controls, it will reflect the values into the receiver web part.
Pass Data Between Web Parts (Connected Web Parts) Using SPFx
Code
The code can be found here.