Featured

How to add Buy me coffee script dynamically into React application or Javascript

Stumble upon in the search of adding Buy me coffee script into react application land to the following wonderful hook solution to add script to react on stack overflow here

I will also add up here the update code of stack overflow which helped in the solution (for incase above shared link will be changed in future, and all the code credit is to the author of stack overflow user)

Update:
Now that we have hooks, a better approach might be to use useEffect like so:

useEffect(() => {
  const script = document.createElement('script');

  script.src = "https://use.typekit.net/foobar.js";
  script.async = true;

  document.body.appendChild(script);

  return () => {
    document.body.removeChild(script);
  }
}, []);
Which makes it a great candidate for a custom hook (eg: hooks/useScript.js):

import { useEffect } from 'react';

const useScript = url => {
  useEffect(() => {
    const script = document.createElement('script');

    script.src = url;
    script.async = true;

    document.body.appendChild(script);

    return () => {
      document.body.removeChild(script);
    }
  }, [url]);
};

export default useScript;
Which can be used like so:

import useScript from 'hooks/useScript';

const MyComponent = props => {
  useScript('https://use.typekit.net/foobar.js');

  // rest of your component
}

Solution, I have added on top a ‘config’ param to the useScript function for easy BMC Buy me coffee widget attributes to get added to script object dynamically:

const useScript = (url, config = null) => {

  useEffect(() => {
    const script = document.createElement("script");

    script.src = url;
    script.async = true;

    if (config && Object.keys(config).length) {
      for (let key in config) {
        script.setAttribute(key, config[key]);
      }
    }

    // console.log(script);
    document.body.appendChild(script);

    return () => {
      document.body.removeChild(script);
    };
  }, [url]);
};

export default useScript;

Boom!

Featured

Aliases destructuring ES6 example in JavaScript

Straight to the example of giving another name to destructure literals from the javascript object.

let obj1 = {
a: 'mercy',
b: 'grace',
c: 'blessing'
}

let obj2 = {
a: 'cool',
b: 'awesome',
c: 'rastafaria'
}

//destructuring ES6 way obj1: 
const {a, b, c} = obj1;

//destructuring ES6 way obj2: (Oops same keys error if using obj1 and obj2 in same file, so saviour is aliases aliases aliases!)
const { a: newA, b: newB, c: newC } = obj2;

Thanks for checking here! hope it solves your search and save your time! 

How to play with Graphql playground for Mutation’s?

Okay, well I was also looking for solution to this and here it is:

Mutation example of adding employee using Graphql Playground, most important step after you have added the code below to playground tab window:

# Write your query or mutation here
mutation AddEmployee(
  $name: String!,
  $phoneNumber: String!,
  $email: String! ,
  $designation: String,
  $image: String) {
    addEmployee(employee: 
      { name: $name, phoneNumber:$phoneNumber, email: $email, designation: $designation, image:$image },
    ) 
      {
        emp_id
        name
      }
    
}

You need to add your JSON payload to QUERY VARIABLES tab window (which is closed by default on below main playground window, without this input payload you will not see results, but error. (keep an eye on here while you make mutation test in graphql playground)

grapql-mutation-example-with-query-variable-input-parameters

Thanks for visiting!

in React or Javascript error: Invalid attempt to destructure non-iterable instance. In order to be iterable, non-array objects must have a [Symbol.iterator]() method.

If you are facing similar problem, this is coming up due to you miss the syntax of destructing or you have wrongly typed out your code, as I was doing, fix is below.

Example error code:

const [ value, name ] = e.target;

Should be:

const { value, name } = e.target;

Actually solution we could have found from error description itself, if we have glance properly to error text rather just copy paste it to search.

Anyways, its happens and we learn like this.

Thanks and enjoy.

PHP sql returns space in email address while query with json post data using post method

I have faced a scenario where I was getting space error in the email address before @ symbol in email address, as I were making simple post request to verify the login.
Json post object sample:

{
	"email": "test@example.com",
  	"password" : "test123"
}

Was returning error in response of like test @example.com so the email address was not getting verified in the database.

Upon google came to following stack overflow result for solution but not satisfy with the approach of replacing single quote again and forcing self to post the email address with single quote in post request, which is obviously not a good solution. (later I have commented on the result page with my solution 😉 )
Simply I did this at query level and it resolve the space issue in the email address while query in database.

$query = "SELECT * FROM 
            " . $this->table_name . " 
            WHERE email =  '".$this->email."'  LIMIT 0,1";

Finally, wrapped the email address variable with the single quotes, and resolved the issue of space before @ symbol.

Hope this solution and result page will save your day!

npm install error due to typescrip dev dependency or following error

If you face any similar kind of log error during npm install for react:

7090 verbose stack FetchError: invalid json response body at https://registry.npmjs.org/@typescript-eslint%2ftypescript-estree reason: Unexpected end of JSON input7090 verbose stack     at C:\Users\HP\AppData\Roaming\npm\node_modules\npm\node_modules\minipass-fetch\lib\body.js:77:317090 verbose stack     at async Promise.all (index 60)7090 verbose stack     at async Arborist.[buildDepStep] (C:\Users\HP\AppData\Roaming\npm\node_modules\npm\node_modules\@npmcli\arborist\lib\arborist\build-ideal-tree.js:854:5)7090 verbose stack     at async Arborist.buildIdealTree (C:\Users\HP\AppData\Roaming\npm\node_modules\npm\node_modules\@npmcli\arborist\lib\arborist\build-ideal-tree.js:209:7)7090 verbose stack     at async Promise.all (index 1)7090 verbose stack     at async Arborist.reify (C:\Users\HP\AppData\Roaming\npm\node_modules\npm\node_modules\@npmcli\arborist\lib\arborist\reify.js:127:5)7090 verbose stack     at async install (C:\Users\HP\AppData\Roaming\npm\node_modules\npm\lib\install.js:38:3)7091 verbose cwd C:\Users\HP\Documents\team-captain\frontend7092 verbose Windows_NT 10.0.190427093 verbose argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\HP\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js" "install" "typescript" "--save-dev"7094 verbose node v12.17.07095 verbose npm  v7.5.27096 error code FETCH_ERROR7097 error errno FETCH_ERROR7098 error invalid json response body at https://registry.npmjs.org/@typescript-eslint%2ftypescript-estree reason: Unexpected end of JSON input7099 verbose exit 1

run following command to clean up npm cache or manually delete the npm-cache folder using path from the above log generation in command line interface.
or update your npm version to the latest version with using following command:
npm install -g npm@latest(or specific to latest version)
like here