Back to List

Using React Hooks in the SharePoint Framework (SPFx 1.8)

Sarah Otto Sarah Otto  |  
May 02, 2019
 

A new feature released in React 16.8 is React Hooks that allow you to reuse stateful logic and share component behavior among many components or with the community. Before React Hooks, we broke down our UI into small, digestible, reusable components. The idea was that each component could be pulled out and reused in other projects. Each component handled its own state and managed its behaviors. But it was hard to share similar behaviors amongst other components. There are a lot of great resources already out on React Hooks that describe what they are and how to utilize a hook. Here are a few I have found helpful to get started:

  1. Making Sense of React Hooks
  2. Introducing React Hooks
  3. Composing Behavior with React Hooks

Our team develops web parts and business applications for our clients in SharePoint Online utilizing SPFx. We got really excited about this news and saw plenty of use cases for it. But then we realized the Microsoft SPFx generator does not come with the latest version of React. The newest update to the SharePoint Framework 1.8 comes with React 16.7.

In this blog, I'm going to show how you can get your hands dirty using React Hooks with SPFx.
 

1. Run the Generator

You'll want to set up your development environment and run the Microsoft SharePoint Generator to get the latest update. Check out this article for getting started with the SharePoint Framework.

In a console, run:

npm install @microsoft/generator-sharepoint

yo @microsoft/sharepoint

Complete the prompts for the Yeoman generator, and make sure you select React Framework!

Run a gulp build and make sure the default generator builds properly. If you are successful, keep going! Otherwise, check that you have the proper development environment set up.
 

2. Update Your Version of React

Looking at your package.json, you will see the project was created with React 16.7. We'll want to update that package to use at least React 16.8. In a console, run:

npm install react@latest react-dom@latest --save

gulp build

Verify that react and react-dom dependencies were updated to ^16.8.X and your project builds.
 

3. Update Your React Types

To use the features of React 16.8, you also need to update your React Types. The currently installed version with the generator is @types/react 16.4.2. In a console, run:

npm install @types/react@latest @types/react-dom@latest --save-dev

gulp build
 

4. Build and Panic!

At this point, if we run another gulp build, you're going to see a lot of build errors! But try not to panic!

gulp build and panic
 

Our currently installed version of TypeScript is not compatible with the latest React types we just installed. Luckily, with SPFx 1.8 we have the ability to specify which version of TypeScript we want to use by installing @microsoft/rush-stack-compiler and specifying a version of TypeScript.

In a console, run:

npm install @microsoft/rush-stack-compiler-3.3 --save-dev

npm install typescript --save-dev

gulp build


You can find more detailed instructions by Elio Struyf at Define the TypeScript Version you want to use in the SharePoint Framework.

NOTE: If you're following this, but not on the latest SPFx 1.8, there are other methods to resolving these error types since you cannot yet define the TypeScript version.
 

4. Update Your tsconfig File

Your tsconfig.json file is still pointed at an older version of the @microsoft/rush-stack-compiler. We'll need to replace our "extends" property to match the @microsoft/rush-stack-compiler of the typescript version we installed.

"extends": "./node_modules/@microsoft/rush-stack-compiler-3.3/includes/tsconfig-web.json"

update tsconfig file
 

5. Update Your Gulp File

We'll also want to update our gulpfile.js file. Without updating this file, SharePoint is going to pull React from the version that comes with SharePoint Online and not include any React dependencies in your bundle. To pull from our installed version of React, we update our configuration as follows:

build.configureWebpack.mergeConfig({
    additionalConfiguration: (generatedConfiguration) => {
        generatedConfiguration.externals = generatedConfiguration.externals
            .filter(name => !(["react", "react-dom"].includes(name)))
        return generatedConfiguration;
    }
});

Warning! By doing this, your bundle will be larger and your load times may not be as performant. Typically, this is unnoticeable based on the performance of SharePoint.

 

6. Build Your Project gulp build

Run a build of your project gulp build in the console. We should have a clean, successful build, and we should see our updated typescript version.

gulp project build
 

7. Use a Hook!

Now you should be at a point in your setup to utilize React 16.8 features. You should be able to create your functional components and utilize hooks built by you, the community or React. 

That's it! If you have any questions or experiences using React Hooks, let us know in the comments.
 

SharePoint

 

Love our Blogs?

Sign up to get notified of new Skyline posts.

 


Related Content


Technology for the Modern Workplace at Business Breakfast Bytes
May 21, 2019
Location: Oak Park Place 2205 Midway Road Menasha, WI 54952
Blog Article
SharePoint Workflow "Failed to Start"?
Kyle ZiberKyle Ziber  |  
Oct 19, 2018
After a recent .Net update that was rolled up into a Windows Server update, SharePoint workflows are failing to start. Microsoft is working on a bug fix for this issue but has a workaround to get back up and running quickly.   Two big issues with SharePoint environments Before I start on...
Blog Article
3 Reasons SharePoint Governance Plans Fail
Ben WeldenBen Welden  |  
Sep 11, 2018
If you’ve been around the SharePoint world for a while, you’re probably familiar with the basic premise of governance: manage the ways that users create, store, and consume content to keep SharePoint from resembling a series of junk drawers.   Recognition of the need for...
Blog Article
3 Key Questions for Choosing Communication Sites or Office 365 Groups
Sarah BuruinSarah Buruin  |  
Jun 12, 2018
In the last few years, Office 365 has added and updated new features and products at what feels like a breakneck pace. It’s left many of us in the SharePoint community trying to understand how to handle this new world and stay on track using the right new features and technologies...
Blog Article
Deploying Your First SharePoint Framework Webpart
Brad SchlintzBrad Schlintz  |  
Mar 13, 2018
Welcome to Part 2 of our SharePoint Framework series. Part 1 of the series gave an overview of the SharePoint Framework including the customization types, toolchain and tips for getting started. We will now talk about deploying our first SharePoint Framework webpart.   Which Deployment...