React Captcha Generator In Spfx


The React Captcha Generator is a plugin written in React.js to generate a captcha in React forms.
Open a command prompt. Create a directory for the SPFx solution.
md spfx-captcha
Navigate to the above-created directory.
cd spfx-captcha
Run the Yeoman SharePoint Generator to create the solution.
yo @microsoft/sharepoint
Solution Name
Hit Enter to have the default name (spfx-captcha 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 be deployed instantly to all sites and be accessible everywhere.
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 not shared 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 - Reactcaptcha
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 project dependencies by running the below command,
npm shrinkwrap
In the command prompt, type the below command to open the solution in the code editor of your choice.
NPM Packages Used,
On the command prompt, run the below commands
  1. npm i react-captcha-generator  
Necessary imports for captcha plugin:
  1. import  ReactCaptchaGenerator from 'react-captcha-generator';  
To initialize captcha, insert the below tag in render method:
  1. <ReactCaptchaGenerator result={this.result} />  
Full Code
  1. import * as React from 'react';  
  2. import { IReactcaptchaProps } from './IReactcaptchaProps';  
  3. import  ReactCaptchaGenerator from 'react-captcha-generator';  
  4. export interface ICaptchaState {      
  5.   captcha?:string;    
  6. }   
  7. export default class Reactcaptcha extends React.Component<IReactcaptchaProps, ICaptchaState> {  
  8.   private  recaptchaRef = React.createRef<HTMLInputElement>();  
  9.   constructor(props) {  
  10.     super(props);  
  11.     this.state = {  
  12.       captcha: '',  
  14.     };  
  15.     this.check = this.check.bind(this);  
  16.     this.result = this.result.bind(this);  
  17.     this.submitform = this.submitform.bind(this);  
  18.   /*  this.resetclick = this.resetclick.bind(this);*/  
  19.   }  
  20.   public render(): React.ReactElement<IReactcaptchaProps> {  
  22.     return (  
  23.       <div className="MyApp">  
  24.         <ReactCaptchaGenerator result={this.result} />  
  25.       <form onSubmit={this.submitform}>  
  26.           <input type='text' className={'myinput'} ref={this.recaptchaRef} />  
  27.           <input type='submit' />  
  28.         </form>  
  29.     </div>  
  30.     );  
  31.   }  
  32. /* public resetclick(e) { 
  33.     e.preventDefault(); 
  34.    // this.forceUpdate();  
  36.   }*/  
  37.   public submitform=(e) =>{  
  38.     e.preventDefault();  
  39.     this.check();  
  40.   }  
  42.   public result=(text)=> {  
  43.     this.setState({  
  44.       captcha: text  
  45.     });  
  46.   }  
  48.   public check=()=> {  
  49.    console.log(this.state.captcha, this.recaptchaRef.current.value, this.state.captcha === this.recaptchaRef.current.value);  
  50.   }  
  51. }  
Since the author of the plugin only focused on componentwillmount method and not on componentdidupdate method, we are unable to rerender the component (i.e. not able to refresh the captcha; if we want to refresh the captcha we need to reload the enter page).
Output of captcha Plugin,
React Captcha Generator In Spfx
Captcha Verification in console output.
Wrong captcha 
React Captcha Generator In Spfx
Correct Captcha
React Captcha Generator In Spfx


Hence we learned about initializing captcha in Spfx form using the React Captcha Generator plugin. I hope this helps someone. Happy coding :)