Hooks Overview

React Hooks are a way to add React.Component features to functional components. Features include:
  • State
  • Lifecycle
Hooks let you use React’s features without classes.

What is this useState() syntax?

You may be unfamiliar with the useState() syntax. This uses destructuring assignment for arrays. It is similar to how we would pull props out an object with object destructuring.

What does useState() give us?

useState gives us two variables. We can name our two variables whatever we want, just know that:
  • The first variable is the value. Similar to this.state
  • The second variable is a function to update that value. Similar to this.setState
  • The final part of useState is the argument that we pass to it. The useState argument is the initial state value. In the case of our counter, we started at 0.

What’s wrong with classes?

The React Hooks intro gives a good section on this: Classes confuse both people and machines. Classes can sometimes be confusing and be written in any number of ways. Dive into somebody else’s project and you could be in for a world of different syntax and style choices. Though there are no plans to remove class support. we just have another way to code.
By allowing classes to be converted into smaller functional components, we can further break out parts of our application into more focused components.

React’s Effect Hook

The State Hook allows us to use state in React functional components. This gets us a step closer to using functional components over class components. The next part of moving to functional components is lifecycle methods.
Effects are similar to componentDidMount, componentDidUpdate, and componentWillUnmount
This is what the Effect Hook is for. Side-effects are things you want your application to do such as:
  • Fetch data
  • Manually change the DOM (document title)
  • Set up a subscription
  • Run effects after every render

Running an Effect Hook only when something changes

Since useEffect() runs every time a component renders, how do we get it to only run once on mount? The Effect Hook can take a second argument, an array. It will look through the array and only run the effect if one of the values has changed.
What about componentWillUnmount()
To run something before a component unmounts, we just have to return a function from useEffect()

Using State and Effects Together

There’s no problem using them together! Together they can create functional components that work the same as your class components!
Open a command prompt. Create a directory for SPFx solution.
md spfx-React-Hooks
Navigate to the newly created directory above.
cd spfx-React-Hooks
Run the Yeoman SharePoint Generator to create the solution.
yo @microsoft/sharepoint
Solution Name
Hit Enter to have default name (spfx-pnp-DropDown in this case) or type in any other name for your solution.
Selected choice – Hit Enter
Target for the component
Here, we can select the target environment where we are planning to deploy the client web part, 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 same folder or create a subfolder for our solution.
Selected choice – Same folder
Deployment option
Selecting Y will allow the app to deploy instantly to all sites
Selected choice –  N (install on each site explicitly)
Permissions to access web APIs
Choose if the components in the solution require permission to access web APIs that are unique and unshared with other components in the tenant.
Selected choice – N (solution contains unique permissions)
Type of client-side component to create
We can choose to create a client-side web part or an extension. Choose the web part option.
Selected choice – WebPart
Web part name
Hit Enter to select the default name or type in any other name.
Selected choice – SpfxReactHooksCrud
Web part description
Hit Enter to select the default description or type in any other value.
Framework to use
Select any JavaScript framework to develop the component. Available choices are – No JavaScript Framework, React, and Knockout.
Selected choice – React
The yeoman generator will perform a 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 the project dependencies by running below command.
npm shrinkwrap
In the command prompt, type the below command to open the solution in the code editor of your choice.
code .
NPM Packages Used,
On the command prompt, run below command.
npm i @pnp/logging @pnp/common @pnp/odata @pnp/sp –save
for Polyfills
npm install –save @pnp/polyfill-ie11
in SpfxReactHooksCrudWebPart.ts,
in HookUsestate.ts

in ISpfxReactHooksCrudProps.ts

in SpfxReactHooksCrud.tsx


in MyList.tsx


in mySpListCOllection.tsx

Here, I am using “ProjectStatus” as a list name with “MileStone,Id,Title” as Field Names.