Showing posts with label JQUERY. Show all posts
Showing posts with label JQUERY. Show all posts

Tuesday, October 20, 2020

Using Redux in a React Native App

Redux is a library for state management that ensures that the application logic is well-organized and that apps work as expected. Redux makes it easy to understand your application's code regarding when, where, why, and how the state of the application is updated.

Redux is made up of the following key parts:

  • actions
  • reducers
  • store
  • dispatch
  • selector

In this post, we'll look at each part of the Redux architecture in turn.

Actions and Action Creators

An action is a plain JavaScript object that has a type field and an optional payload. It can also be thought of to as an event that describes something that has happened.

Action creators are just functions that create and return action objects.

Reducers

reducer is also a function that receives the current state and an action object, updates the state if necessary, and returns the new state. A reducer is not allowed to modify the existing state; instead, it copies the existing state and changes the copied values. In other words, the reducer should be a pure function.

Here is an example. 

Store

A store is an object that stores the current state of a Redux application. You create the initial state of the store by passing a reducer to the configureStore functIon.

To get the current state of a store, use the getState() function as above.

Dispatch and Selectors

dispatch() is a Redux store method and is used to update the state by passing an action object. Here, we create a task/taskAdded action with the addTask() action creator, then pass it to the dispatch() metdho.

Selectors are used for reading data from a store.

Tutorial on Using Redux With React Native

In this React Native Redux tutorial, you will develop an app that allows a user to choose from a given set of subjects. The app will contain two screens and you will also learn how to navigate between them using react-navigation

Create a New React App

Create a new project using Expo CLI tool

Currently, you have a single component  App.js displaying a welcome message. 

Create two new components in Home.js and Subjects.js The home component will be the first screen a user sees, and the subjects component will display all the subjects in our app. Copy the code from App.js and add it to Home.js and Subjects.js.

Modify Home.js and Subjects.js to use Home and Subjects instead of the App class.

Subects.js should look similar.

Navigating Between Screens

Currently, there is no way to navigate between the two screens. The stack navigator allows your app to transition between screens where each new screen is placed on top of a stack.

First, install @react-navigation/native and its dependencies.

For an Expo managed project, install the following.

Go to App.js and import react-native-gesture-handlerat the top. You also need to add  NavigationContainer and createStackNavigator to App.js. Then, to add navigation capabilities, wrap the components inside the App class in a NavigationContainer component. 

Now the navigator is aware of your screens, and you can provide links for transitioning between screens.

Redux in a React Native App

As we mentioned earlier, Redux is a state management tool, and you will use it to manage all the states of the application. Our application is a subject selector where the user chooses the subjects from a predefined list.

First, install the Redux packages.

A Redux app follows the following conditions:

  • The state completely describes the condition of the app at any point in time.
  • The UI is rendered based on the state of the app. 

For example, when a user clicks a button, the state is updated, and the UI renders the change based on the state.

Create a Reducer

The reducer will be responsible for keeping the state of the subjects at every point in time.

Create the file SubjectsReducer.js at the root level and add the following code.

In this file, you create an INITIAL_STATE variable with all the curriculum subjects and export the subjects reducer as a property called subjects. So far, the subjects reducer doesn't respond to any actions—the state can't change.

Create an Action

An action has a type and an optional payload. In this case, the type will be SELECT_SUBJECT, and the payload will be the array index of the subjects to be selected.

Create the SubjectsActions.js file at the root level and add the following code.

Now, we'll update the reducer to respond to this action.

Add the Reducer to the App

Now, open App.js, and create a new store for the app using the createStore() function. We also make that store available to the components in our app by wrapping them in the <Provider> component. This will ensure that the store's state can be accessed by all of the child components.

Add Redux to the Components

Now let's see how to make state data available to components with the connect() function. To use connect(), we need to create a mapStateToProps function, which will maps the state from the store to the props in the two components.

Open Home.js, map the state, and render the values for the current subjects (which will be found in this.props.subjects.current). Also, add a button to navigate to the Subjects component.

Similarly, we'll use connect in the Subjects.js  file to map the state to the component properties. 

We'll also add the code to display all the subjects in the Subjects component and a button to navigate to the Home screen. You will also add a button that will add the addSubject action to Subject.js. Here's what the final render() method in Subject.js should look like this:

The final app should look like this.

redux app

Conclusion

Redux is a valuable tool, but it is only useful when used in the right way. Some of the situations where Redux might be helpful include code that is being worked on by many people or applications that contain large amounts of application state.

In this tutorial, you learned how to set up a React Native app using Redux.

The Best React Native App Templates on CodeCanyon

CodeCanyon is a digital marketplace offers the best collection of React Native themes and templates. With a React Native app template, you can get started building your apps in the fastest time possible.

If you have trouble deciding which React Native template on CodeCanyon is right for you, this article should help: 



by Esther Vaati via Envato Tuts+ Code

Parsing a CSV File With JavaScript

Final product image
What You'll Be Creating

The CSV (Comma Separated Values) file format is a popular way of exchanging data between applications.

In this quick tip, we’ll learn how JavaScript can help us visualize the data of a CSV file.

Creating a CSV File

To begin with, let’s create a simple CSV file. To do this, we’ll take advantage of Mockaroo, an online test data generator. Here’s our file:

Initial-CSV-File

Converting a CSV File Into an HTML Table

Now that we’ve generated the file, we’re ready to parse it and build an associated HTML table.

As a first step, we’ll use jQuery’s ajax function to retrieve the data from this file:

Assuming the AJAX request is successful, the successFunction is executed. This function is responsible for parsing the returned data and transforming them into an HTML table:

So, the idea is to convert each of the CSV rows into a table row. With that in mind, let’s briefly explain how the code above works:

  • First, we use a regex to split the AJAX response, and thus separate the CSV rows.
  • Then, we iterate through the CSV rows and split their data fields.
  • Finally, we loop through the data fields and create the corresponding table cells.

Furthermore, to get a better understanding of this code, consider the following visualization:

CSV-Representation

At this point, it’s important to clarify why we used the /\r?\n|\r/ regex to split the CSV rows.

As you probably already know, there are different representations of a newline across operating systems. For example, on Windows platforms the characters representing a newline are \r\n. That said, by using the regex above, we’re able to match all those possible representations.

In addition, most text editors allow us to choose the format for a newline. Take, for instance, Notepad++. In this editor, we can specify the desired format for a document by navigating to this path:

Notepad++-EOL

To illustrate it, consider our file. Depending on the format we choose, it would look like this:

CSV-EOL

Adding Styles to the HTML Table

Before we look at the resulting table, let’s add a few basic styles to it:

Here’s the generated table:

Generated-Table

How to Parse a CSV File With the PapaParse Library

In this section, we’ll see how you can use the PapaParse library to parse a CSV file in the blink of eye! PapaParse is a really powerful CSV parser which provides you a lot of configuration options and you could use it for really big CSV files as well.

The PapaParse library is available on npm, and if you don’t want to use npm, you can download the official PapaParse nmp package from unpkg instead.

How it Works

The following example demonstrates how easy it is to parse a CSV string.

The results variable holds the following contents.

PapaParse results

As you can see, Results.data holds an array of all the rows. If there are any errors during parsing, they will be in Results.errors. Finally, you can use Results.meta to access meta information about the CSV string.

On the other hand, if you want to directly parse a local CSV file you can pass a JavaScript File object:

And you can also pass in the URL to a remote CSV file:

Apart from basic parsing, PapaParse provides a lot of other features like:

  • streaming large files (so you can process them line-by-line)
  • reverse parsing (to emit CSV from a JavaScript object)
  • jQuery integration
  • and more

I encourage you to explore this library since it’s really powerful and easy-to-use!

Conclusion

In this short article, we went through the process of converting a CSV file into an HTML table. Of course, we could have used a web-based tool for this conversion, yet I think that it’s always more challenging to achieve this by writing your own code.


by George Martsoukos via Envato Tuts+ Code

Product Grid Style 153

The post Product Grid Style 153 appeared first on Best jQuery.


by Admin via Best jQuery

CSS Text Effect Style 87

The post CSS Text Effect Style 87 appeared first on Best jQuery.


by Admin via Best jQuery

Thursday, October 15, 2020

Range Slider Style 80

The post Range Slider Style 80 appeared first on Best jQuery.


by Admin via Best jQuery

Product Grid Style 152

The post Product Grid Style 152 appeared first on Best jQuery.


by Admin via Best jQuery

WebGL Video Transitions with Curtains.js

Some experimental video transitions using Curtains.js and shaders.We’ve done some crazy polygon transitions, and before that some WebGL Image Transitions. But a lot of people have been asking how to translate those techniques into the video world. And I agree, one cannot underestimate the importance of video on the web.

The post WebGL Video Transitions with Curtains.js appeared first on Best jQuery.


by Admin via Best jQuery

Scroll Animations for Image Grids

Some ideas for scroll animations for image grids powered by Locomotive Scroll.

The post Scroll Animations for Image Grids appeared first on Best jQuery.


by Admin via Best jQuery

Monday, October 12, 2020

Submit A Form Without Page Refresh Using jQuery

A great way to improve the user experience of your website is to validate and submit forms without a page refresh.

In this tutorial I'll show you how easy it is to do just that—validate and submit a contact form that without page refresh using jQuery! Let's get started.

What We're Building

In this example, we have a simple contact form with name, email, and phone number. The form submits all the fields to a PHP script without any page refresh, using native jQuery functions.

1. Build the HTML Form

Let's take a look at our HTML markup. We begin with our basic HTML form:

You might notice that I have included a div with id contact_form that wraps around the entire form.

Be sure to not miss that div in your own form as we will be needing this wrapper div later on. You might also notice that I have left both the action and the method parts of the form tag blank. We actually don't need either of these here, because jQuery takes care of it all later on.

Another important thing is to be sure to include the id values for each input field. The id values are what your jQuery script will be looking for to process the form with.

We are also doing some very basic client-side validation using HTML5 attributes like required and minlength. The minlength attribute will make sure that users supply a name that is at least 3 characters long. Similarly, the required attribute makes sure that users fill out all the form values you need. You can read more about these attribute in our tutorial on validating form inputs using only HTML5 and Regex.

I've added some CSS styles to produce the following form:

Contact Form

2. Begin Adding jQuery

The next step in the process is to add some jQuery code. I'm going to assume that you have downloaded jQuery, uploaded to your server, and are referencing it in your webpage.

Next, open up another new JavaScript file, reference it in your HTML as you would any normal JavaScript file, and add the following:

This function runs  soon as the HTML document is ready. If you have done any work in jQuery previously, this function is the same as jQuery's document.ready function. Inside we will set up our validation code.

3. Write Some Form Validation

We will now write some basic from validation using jQuery. This will improve upon the validation we have so far. Using a validation library will gives us more control over the error messages that are shown to users. It also requires minimal or no change in the markup of the form.

Starting by loading the jQuery Validation library on your webpage. Now, just add the following code:

Make sure you pass the right selector when calling the validate() method. This will validate the form without requiring you to write any error messages in the HTML or the logic to display and hide different error messages in JavaScript. Try submitting the form without filling in any values or by knowingly adding wrong input. The form will display a nice error message like the following image.

Form Validation Error Message

Using the validation library will also allow you to add conditional validation logic to your forms. For example, you will be able to add code that requires a phone number only when the email address has not been provided. I have covered this in more detail in the jQuery form validation tutorial.

4. Process Form Submission With the jQuery AJAX Function

Now we get to the heart of the tutorial—submitting our form without page refresh, which sends the form values to a PHP script in the background. Let's take a look at all the code first, then I will break down into more detail next. Add the following code just below the validation snippet we added previously:

There is a lot going on here! Let's break it all down—it's so simple and so easy to use once you understand the process. We first create a string of values, which are all the form values that we want to pass along to the script that sends the email.

This can be achieved pretty easily using the built-in serialize() method in jQuery. This way you don't have to worry about getting and concatenating the value of different valid user inputs yourself.

I've commented out an alert that I sometimes use to be sure I am grabbing the right values, which you may find helpful in the process. If you uncomment that alert and test your form, assuming everything has gone right so far, you should get a message similar to the following:

Datastirng Alert

Now we get to our main ajax function, the star of today's show. This is where all the action happens, so pay close attention!

Basically what's going on in the code is this: The .ajax() function processes the values from our string called dataString with a PHP script called bin/process.php, using the HTTP POST method type. If our script processed successfully, we can then display a message back to the user, and finally return false so the page does not reload. That's it! The entire process is handled right there in these few lines!

There are more advanced things you can do here, other than giving a success message. For example you could send your values to a database, process them, then display the results back to the user. So if you posted a poll to users, you could process their vote, then return the voting results, all without any page refresh required.

Let's summarize what happened in our example, to be sure we have covered everything. We grabbed our form values with jQuery using the serialize() method, and then placed those into a string like this:

Then we used jQuery's ajax() function to process the values in the dataString. After that process finishes successfully, we display a message back to the user and return false so that our page does not refresh:

The success part of the script has been filled in with some specific content that can be displayed back to the user. But as far as our AJAX functionality goes, that's all there is to it. For more options and settings be sure to check out jQuery's documentation on the ajax function. The example here is one of the simpler implementations, but even so, it is very powerful as you can see.

5. Display a Message Back to the User

Let's briefly look at the part of the code that displays our message back to the user, to finish out the tutorial.

First, we change the entire contents of the #contact_form div (remember I said we would be needing that div) with the following line:

What that has done is replaced all the content inside the contact form, using jQuery's html() function. So instead of a form, we now have a new div with id of message. Next, we fill that div with an actual message: an h2 saying Contact Form Submitted:

We'll add even more content to the message with jQuery's append() function, and top everything off we add a cool effect by hiding the message div with the jQuery hide() function, then fade it in with the fadeIn() function:

So the user ends up seeing the following after they submit the form:

Successful Submission

Conclusion

By now, I think you will have to agree that it is incredibly easy to submit forms without page refresh using jQuery's powerful ajax() function. Just get the values in your JavaScript file, process them with the ajax() function and return false. You can process the values in your PHP script just like you would any other PHP file, the only difference being that the user does not have to wait for a page refresh—it all happens silently in the background.

So if you have a contact form on your website, a login form, or even more advanced forms that process values through a database and retrieve results back, you can do it all easily and efficiently with AJAX.

Learn JavaScript With a Free Course

If you want to master JavaScript, be sure to check out our free course to learn the complete A-Z of modern JavaScript fundamentals.

 

In this course, you'll learn all of the essential concepts of the JavaScript language. That's right: all of them! Including the most important recent improvements to the language, in JavaScript ES6 (ECMAScript 2015) and JavaScript ES7 (ECMAScript 2016).

You'll start with the very fundamentals of the language: variables and datatypes. Then in each lesson you'll build knowledge, from data structures like arrays and maps to loops, control structures, and functions. Along with the basics of the language, you'll also learn some key built-in APIs for manipulating data, AJAX, and working with the web browser DOM. Finally, you'll get a look at some of the most powerful and widely used web APIs that are supported by all modern browsers.



by Eric via Envato Tuts+ Code

Link Hover Style 119

The post Link Hover Style 119 appeared first on Best jQuery.


by Admin via Best jQuery

Pagination Style 94

The post Pagination Style 94 appeared first on Best jQuery.


by Admin via Best jQuery

Download and Install WordPress Via the Shell Over SSH and With WP-CLI

Installing WordPress often takes a lot of steps: downloading and uncompressing a zip file, uploading files to the server, and setting up the database and config. That can take a lot of time. Or you can do it by using the Fantastico or SimpleInstall utilities available via your host's control panel. There is nothing wrong with that, but it's not foolproof to say the least. In this post I'll show you a faster and more reliable way—with the shell over SSH.

Nowdays most hosting plans offer an SSH command-line facility so you should definitely consider the SSH method to download and install WordPress on your server. The big advantage  is that you don't need to visit different sites, and don't need to do any uploading or open any control panels. You do everything via a single command-line interface. For this you will need an SSH client. If you are running on Windows go for PuTTY and if you are on Mac you can use Mac's built-in Terminal or iTerm 2. Before we move ahead please make sure your host offers the Bash shell because our commands are configured for that.

At the end of this article, we'll also go through the WP-CLI tool which is an even easier way to download and install WordPress via command line.

1. Connecting to Your Server

Using PuTTY

Open PuTTY and enter your domain name in the box named Host name(or IP address) and enter the port number used to connect to SSH under Port, then click Open. You can even save your site settings by entering a name in the Saved Sessions box and by pressing Save. Next time you can always load the session by selecting your site and clicking Load.

PuTTY will now ask for your username. Enter your username and press Enter. Now you will be asked for your password. Note here that while you are typing your password, you won't see it being typed on the screen. It's hidden for security reasons. Press Enter after you've typed your password and you will be logged on.

Using any other SSH client or Mac Terminal

Enter the following command in your Terminal client to connect to your site's command-line over SSH:

The -p switch tells it to use the port number 22. If your host allows SSH over default port 22, you can omit -p and 22 in the above command, otherwise substitute 22 for your host's SSH port number. After logging in you will see something like this:

That is the shell command prompt where you will be typing all your commands from now on.

2. Downloading WordPress

Now that we have logged in to our SSH server, we need to go to the correct directory where we want to setup our blog. Then we download the files and extract them there. Supposing the directory you want your blog to be installed under is blogdemo residing under the public_html directory, you will use the following command:

Now that we have reached the correct directory we will download WordPress using the wget command.

The above command downloads the latest WordPress install from their server and extracts the file from it into the blogdemo directory. xf, and z are parameters which tell the tar command to extract the contents from the specified file using gzip.

Now after extraction you will find a wordpress directory under the blogdemo directory containing your install. So to shift the files back to where they should be use the following commands:

This command moves the contents of the wordpress directory into the current directory. Anytime you want to check what is in the current directory, type ls.

You can delete both the wordpress directory and the archive file you downloaded if you want using the following commands:

3. Installing WordPress

In this step we will create the database and corresponding user and associate them together. Then we will use the famous 5 minute install of WordPress to finish it off.

Note: Before moving ahead you will need to check whether you have got the privileges to create a database or not. An easy way to check is to go to your phpMyAdmin and check whether you can create a database from there or not. If you can't that means you won't be able to follow this step. You should check with your web host if they allow you to do so or not. Most shared web hosts will let you create a database.

First you need to login to the MySQL command-line using the following command:

After entering this you will be asked for your MySQL password. Type your password and you will be shown a screen like this:

MySQL Login

Now that we have logged in to the MySQL Server, we will first create a database and will grant access to the user to that database. Use the following commands now:

Don't forget the semi-colon at the end of each MySQL command. The first command creates the database. The second command allows the user to connect to the database. The final command grants all privileges to the user for that database. You can test whether your database creation was successful by running this command:

It should say database changed. Now you can exit the MySQL command-line by typing exit.

Now fire up the blog in your browser and run the usual WordPress install and use the database information we used in the third step to setup your wp-config.php and then setup your blog.

Note: New Database User

In our tutorial we are using an already existing database user to connect to the new database. But in case you want separate user for each database you need to create a new user to access that database. Here is how you should do it.

Once you are inside the MySQL shell, use the following commands to create a new user and set its password.

Now go back to Step 3 and run all other commands with this username.

Editing wp-config.php

In our tutorial I have told you that after doing everything on the shell you can directly proceed to the install. But some of you might want to edit wp-config.php to add special settings and code. You can only do that via the shell. While you are in your blog directory at the shell use the following command to fire up the Vim Editor (a command-line shell file editor)

Now you will see something like what's shown below:

Vim Editor

Press the i key to enter insert mode and use arrow keys to move around the file. Once you have made your edits, press the Esc key to exit insert mode. To exit Vim type : and then type wq and press Enter. This will save your changes and quit Vim.

Download and Install WordPress With the WP-CLI Tool

In this section, I'll show you an even better way to download and install WordPress: with the WP-CLI tool. First, we have to install the WP-CLI tool on the sever.

How to Install the WP-CLI Tool

Run the following commands on your server to download, install and configure the WP-CLI tool.

Let's check if the WP-CLI tool is installed successfully by using the following command.

You should see something like this:

Download and Install WordPress

Let's download the latest version of WordPress first place.

IF the download is successful, you'll see something like the following:

So we've downloaded the WordPress code base now.

Next, it's time to create the wp-config.php file. We can do it with the help of the following command. Replace the placeholders with the actual values. I assume that you've already created the database which you would like to use with WordPress.

Finally, let's run the following command which installs WordPress.

And with that, WordPress is installed successfully on your server! 

In fact, the WP-CLI tool is capable of doing a lot more than just installation. It allows you to manage plugins, themes and do necessary version updates as well. All in all, it's a great tool for WordPress developers, and I would encourage you to explore it! You can learn about WP-CLI here at Envato Tuts+.

Learn to Code PHP for WordPress With a Free Online Course

If you want to learn to code PHP WordPress, check out our free online course on learning to code PHP for WordPress!


 

In this course, you'll learn the fundamentals of PHP programming. To get started, you'll learn why WordPress uses PHP, how to create a PHP file, and how to mix HTML and PHP.  You'll go on to learn how PHP works and how to write simple PHP loops and functions. To wrap things up, you'll build a custom archive page to practice what you learned.

If you want to learn WordPress plugin development, we have a free course for that as well!


by Navjot Singh via Envato Tuts+ Code