Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Reusable Helper Functions in React.js

I've been building React components for my latest app. I know I can reuse components which has helped keep my code DRY.

I wanted to know if I could reuse functions. I know there has to be a way.

Right now I have three components using a password validation function.

passwordValidation() {
  const length = this.state.password.length;
  if (length > 7) return 'success';
  else if (length > 4) return 'warning';
  else if (length > 0) return 'error';
}

I created a helper file - helpers.jsx and added:

export function passwordValidation() {
  const length = this.state.password.length;
  if (length > 7) return 'success';
  else if (length > 4) return 'warning';
  else if (length > 0) return 'error';
}

I then imported it into my component

import { passwordValidation } from '../helpers.jsx'

I keep getting the error "passwordValidation is not a function" when I try binding "this" in my constructor.

If I invoke it in the input tag, I am getting "cannot read property state of undefined."

Just trying to see where I am going wrong. Everything works if I define it in my class and add this.passwordValidation = this.passwordValidation.bind(this).

I'll go back to what I was doing if this isn't best practice, but I'm under the assumption that I should be able to import functions to make life easier and my code cleaner!

like image 780
hancho Avatar asked Oct 19 '17 21:10

hancho


People also ask

How do you create a helper function in react JS?

Then use it like this: MyHelperFunc_1(my_data, function(response){ alert(response. message); // display the response });

How do you write a reusable React component?

First we have to import the component into where we want to reuse it, as you can see in the first line of the above code where we have our import statement. Second, because we passed in name and imageUrl as props in the author component earlier, the author component expects the same data to be passed into it at first.

How do I export more than one function in React?

Use named exports to export multiple functions in React, e.g. export function A() {} and export function B() {} . The exported functions can be imported by using a named import as import {A, B} from './another-file' . You can have as many named exports as necessary in a single file.


1 Answers

Helper functions should not depend on the context of the component they are called to (at least in my opinion). If you need to use some parameter in your function passing that to function is always a better practice since it helps on re-usability. The key for the state property might be different for all components and this might lead to errors if you forget to use exact key for the state property.

For Example

export function passwordValidation(password) {
  const length = password.length;
  if (length > 7) return 'success';
  else if (length > 4) return 'warning';
  else if (length > 0) return 'error';
}

If I change function like above I can use all the given examples below.

import { passwordValidation } from '/path/to/helper/functions.js';

console.log(passwordValidation(this.state.password));
console.log(passwordValidation(this.state.confirmPassword));
console.log(passwordValidation(this.props.password));
console.log(passwordValidation(someFetchedObject.user.password));
like image 81
bennygenel Avatar answered Sep 30 '22 12:09

bennygenel