Open command prompt. Create a directory for SPFx solution.
Navigate to the above created directory.
Run Yeoman SharePoint Generator to create the solution.
Yeoman generator will present you with the wizard by asking questions about the solution to be created.
Selected choice: Hit Enter
Target for component: Here, we can select the target environment where we are planning to deploy the client webpart i.e. SharePoint Online or SharePoint OnPremise (SharePoint 2016 onwards).
Selected choice: SharePoint Online only (latest)
Place of files: We may choose to use the current folder or create a subfolder for our solution.
Selected choice: Use the current folder
Deployment option: We may choose to allow the tenant admin the choice of being able to deploy the solution to all sites immediately without running any feature deployment or adding apps in sites.
Selected choice: N (install on each site explicitly)
Permissions to access Web APIs: We may choose the components in the solution require permissions to access web APIs that are unique and not shared with other components in the tenant.
Selected choice: N (No explicit permissions are required)
Type of client-side component to create: We can choose to create client side webpart or an extension. Choose webpart option.
Selected choice: WebPart
Web part name: Hit Enter to select the default name or type in any other name.
Selected choice: ReactAutoBind
Web part description: Hit Enter to select the default description or type in any other value.
Selected choice: Auto bind events in React
Framework to use: Select any JavaScript framework to develop the component. Available choices are - No JavaScript Framework, React, and Knockout.
Selected choice: React
Yeoman generator will perform scaffolding process to generate the solution. The scaffolding process will take a significant amount of time.
Once the scaffolding process is completed, lock down the version of project dependencies by running the below command.
In the command prompt, type below command to open the solution in a code editor of your choice.
Development Scenario
We will add a textbox and button to our webpart. On the click of button, we will display a greeting message to text entered in textbox.
Define a State
Let us define a state to store the user entered text. Add a file IReactAutoBindState.ts under the “\src\webparts\reactAutoBind\components\” folder.
- export interface IReactAutoBindState {
- userText: string;
- }
Add Controls to Web Part
Open ReactAutoBind.tsx under “\src\webparts\reactAutoBind\components\” folder and add the below imports.
-
- import { TextField } from 'office-ui-fabric-react/lib/TextField';
-
-
- import { IButtonProps, DefaultButton } from 'office-ui-fabric-react/lib/Button';
Define the textbox and button controls.
- public render(): React.ReactElement<IReactAutoBindProps> {
- return (
- <div className={styles.reactAutoBind}>
- <div className={styles.container}>
- <div className={styles.row}>
- <div className={styles.column}>
- <span className={styles.title}>Welcome to SharePoint!</span>
- <p className={styles.subTitle}>Autobind events demo</p>
-
- <TextField
- required={true}
- name="txtSearchText"
- placeholder="Search..."
- value={this.state.userText}
- onChanged={e => this.setState({ userText: e })}
- />
-
- <DefaultButton
- data-automation-id="search"
- target="_blank"
- title="Greet"
- onClick={this.greetButtonClicked}
- >
- Greet
- </DefaultButton>
-
- <div>{this.state.userText}</div>
-
- </div>
- </div>
- </div>
- </div>
- );
- }
Define button click event.
- private greetButtonClicked(): void {
- alert("Hello " + this.state.userText);
- }
Define constructor to set the initial state.
- constructor(props: IReactAutoBindProps, state: IReactAutoBindState) {
- super(props);
-
- this.state = {
- userText: ""
- }
- }
Run the SPFx WebPart
- On the command prompt, type “gulp serve”.
- Open SharePoint site.
- Navigate to /_layouts/15/workbench.aspx.
- Add the webpart to page.
- Type in some text to the textbox.
- Click the "Greet" button.
We expect to see the alert message greeting the text entered in the textbox. But nothing happens. If we observe the developer toolbar console, we have an error “Cannot read property 'state' of undefined at ReactAutoBind.greetButtonClicked”. This means our event has not been bound to our button control.
Bind event to button
In the constructor, add the below code.
- constructor(props: IReactAutoBindProps, state: IReactAutoBindState) {
- super(props);
-
- this.state = {
- userText: ""
- }
-
- this.greetButtonClicked = this.greetButtonClicked.bind(this);
- }
Refresh the SharePoint workbench. Add some text to textbox and click "Greet" button. As the event is bound to the button, we see an alert.
Binding all events at once
There are chances that we forget to add the bindings in constructor for our control. As the number of controls grows on the web part, it might be a huge code for only event binding. To handle this in a graceful way, we can use autobind.
Add the below import.
- import { autobind } from 'office-ui-fabric-react';
Remove any bindings from Constructor.
Decorate event with @autobind.
- @autobind
- private greetButtonClicked(): void {
- alert("Hello " + this.state.userText);
- }
Summary
It is necessary to bind actions to controls. An action can be bound in a constructor individually to the control. Autobind helps to bind the action to control by decorating the method.