Professional Documents
Culture Documents
createElement() :
The first argument to React.createElement() method is a string that represents the HTML
element type we want to create.
The second argument to React.createElement() method is a JavaScript object that contains
all the attributes we want the HTML element to have.
To add a class to a React element, we will have to use className instead of class, as class
is a reserved keyword in JavaScript which has some special meaning.
JSX Expression:
We can write JavaScript inside JSX by writing any valid JavaScript expressions inside curly
braces within JSX.
Component :
A component is a piece of UI that we can reuse. A component holds its functionalities
together, so that it can be seen as an independent unit. A component should ideally address
a specific concern and not hold too many responsibilities.
We should choose a component such that it is as small as possible and ideally only one
responsibility should be associated with one component.
function Box() {
return (
<div className='box'>
I am a box
</div>
);
}
While defining a component, we return some JSX code which represents some HTML code.
The JSX eventually gets converted to React.createElement() function call that returns a
React Element.
We can categorise states mainly into two types: Application State and Component State.
When states are defined in the top most component, we call it application State.
States can be passed down the component tree from a top level component to the child
components via props. If states are defined to a child level component, there is no way it can
be shared to other top level components. States should be kept together and managed as
high up in the application as possible, so that all components have access to it.
We have used the map() method to create JSX tags out of each element of the array. The
map() method creates a new array populated with the results of calling a provided function
on every element in the calling array.
To call a function from a child component that is defined in the parent component, the
function is passed down from the parent component to the child component via props.
React
We install Create React App from Node Package Manager (npm).
The npm start command uses Babel to convert all the JSX code into browser
understandable JavaScript code. It temporarily starts a web server of its own, runs the code,
and displays the output on the browser.
Each module must contain "import React from 'react';" at the beginning of the file so that we
can use functions related to React.
To import a module into another module, we have to add this line at the beginning of the file:
import component_name from 'relative/path/of/the/file';
React
React is a JavaScript library.
React is a core library and React-dom is a website specific.
Instead of writing HTML directly, we will be writing some JavaScript code that uses the
functions provided by React library to create HTML elements for our web page.
React.createElement() is used to create a React element.
As we are dealing with a JSX file - so we must tell it to our browser. We will change the type
text/javascript to text/babel so that the browser understands that it needs to first convert the
JSX file into JavaScript using the Babel compiler.
JSX accepts any valid JavaScript expressions if we put them inside curly braces.
{3+2} is a JavaScript expression that evaluates to 5.
The React.createElement() method takes the nested elements as arguments.
We have to pass the top level component to the ReactDOM.render() method.
We can define a function using both a function and a class.
We have to write the name of the component in a capitalized form, so that when we use a
component the browser can distinguish it with other HTML elements.
Since, we include components using custom tags and not with function calls, props are
passed to components like HTML attributes.
Since props are variables, they can be used inside the JSX using the JSX expression, that
is, inside the curly braces.
Props are uneditable.
Whenever a component needs a state, we must define it as a Class component.
States need to be assigned explicitly in a Class Component, but Props are automatically
assigned.
When we pass a function to another component via props, we must bind the function to this
object. This is done, so that, inside the function this should continue to refer to the parent
component object.
A class component must extend React.component. A class component must have the
render() method. And the render() method should contain a return statement to return the
required JSX.
Each module must contain "export default component_name" at the end of the file so that
these modules can be imported and used into other modules.
To build a React project, we use npm run build from inside the project folder.
React
We have used the <nav> tag as per Bootstrap's recommendation to create the header.
We have used Bootstrap's Breadcrumb to show the user's location on the website.
We have used Bootstrap's Carousel in the property details page to display property images.
The properties and the cities tables have one to many relationship. One property can be
present in only one city, whereas one city can have multiple properties.
We created a separate table to store the properties 'interested users' information:
interested_users_properties.
The logout functionality doesn't need Ajax, it simply is a click away to logout.php page.
We used the base_path variable inside utils.js to add domain to the urls based on
development and production environment.