diff --git a/react_tour/src/App.js b/react_tour/src/App.js index 5bd7862..f98c5d4 100644 --- a/react_tour/src/App.js +++ b/react_tour/src/App.js @@ -1,13 +1,17 @@ - -import MyComponent from './components/Component/co'; -import Component2 from './components/co2/Component2'; - +import MyComponent from "./components/Component/co"; +import Component2 from "./components/co2/Component2"; +import Jsx from "./Jsx/jsx"; +import Example from "./exampleState/exampleState"; +import ExampleWithManyStates from "./exampleState/multipleState"; function App() { return (
- - + + + + +
); } diff --git a/react_tour/src/Jsx/README.md b/react_tour/src/Jsx/README.md new file mode 100644 index 0000000..2f7dd7a --- /dev/null +++ b/react_tour/src/Jsx/README.md @@ -0,0 +1,40 @@ +What is jsx, +How jsx works and +What is really done behind the scene after writing jsx + +React is an Open Source view library created and maintained by Facebook. It's a great tool to render the User Interface (UI) of modern web applications. + +React uses a syntax extension of JavaScript called JSX that allows you to write HTML directly within JavaScript. This has several benefits. It lets you use the full programmatic power of JavaScript within HTML, and helps to keep your code readable. For the most part, JSX is similar to the HTML that you have already learned + +Because JSX is a syntactic extension of JavaScript, you can actually write JavaScript directly within JSX. To do this, you simply include the code you want to be treated as JavaScript within curly braces: { 'this is treated as JavaScript code' } + +However, because JSX is not valid JavaScript, JSX code must be compiled into JavaScript. The transpiler Babel is a popular tool for this process + +JSX stands for JavaScript XML. +JSX allows us to write HTML in React. +JSX makes it easier to write and add HTML in React. + + +Example of simple JSX + +```JS +const JSX =

+Hello JSX! +

; +``` + +Example of a Complex JSX where JSX can represent more complex HTML as well. + +Nested JSX it must return a single element this one parent element would wrap all of the other levels of nested elements. +```JS +const JSX = +
+

Nested JSX

+

This is our example

+ +
+``` \ No newline at end of file diff --git a/react_tour/src/Jsx/jsx.js b/react_tour/src/Jsx/jsx.js new file mode 100644 index 0000000..a17ac06 --- /dev/null +++ b/react_tour/src/Jsx/jsx.js @@ -0,0 +1,7 @@ +import '../Style/jsx.css' +function Jsx() { + const myelement =

Example of JSX!

; + return
{myelement}
; +} + +export default Jsx; diff --git a/react_tour/src/Style/components.css b/react_tour/src/Style/components.css new file mode 100644 index 0000000..0dfcfcb --- /dev/null +++ b/react_tour/src/Style/components.css @@ -0,0 +1,4 @@ +.comp{ + background-color: rgb(96, 221, 96); + margin: 50px; +} \ No newline at end of file diff --git a/react_tour/src/Style/exampleState.css b/react_tour/src/Style/exampleState.css new file mode 100644 index 0000000..32c0094 --- /dev/null +++ b/react_tour/src/Style/exampleState.css @@ -0,0 +1,4 @@ +.exState{ + background-color: aqua; + margin: 50px; +} \ No newline at end of file diff --git a/react_tour/src/Style/jsx.css b/react_tour/src/Style/jsx.css new file mode 100644 index 0000000..c013186 --- /dev/null +++ b/react_tour/src/Style/jsx.css @@ -0,0 +1,4 @@ +.exjsx{ + background-color: yellow; + margin: 50px; +} \ No newline at end of file diff --git a/react_tour/src/components/Component/co.js b/react_tour/src/components/Component/co.js index a859f97..d0e3cae 100644 --- a/react_tour/src/components/Component/co.js +++ b/react_tour/src/components/Component/co.js @@ -1,13 +1,12 @@ -import React from 'react'; +import React from "react"; +import '../../Style/components.css' class MyComponent extends React.Component { - constructor(props) { - super(props); - } - render() { - return ( -

This is example of Class component

- ); - } - }; + constructor(props) { + super(props); + } + render() { + return

This is example of Class component

; + } +} - export default MyComponent; \ No newline at end of file +export default MyComponent; diff --git a/react_tour/src/components/README.md b/react_tour/src/components/README.md index 473d156..4e1a0d9 100644 --- a/react_tour/src/components/README.md +++ b/react_tour/src/components/README.md @@ -8,23 +8,21 @@ There are two ways to create a React component. (1) The first way is to use a JAVASCRIPT FUNCTION. Example; -```JS function Component2() { return (
-

This is Function component

+

This is Function component

); } export default Component2 -``` To create a component with a function, you simply write a JavaScript function that returns either JSX or null. One important thing to note is that React requires your function name to begin with a CAPITAL letter. Above is an example of a stateless functional component that return div element that contain head 1 element (2)The other way to define a React component is with the ES6 CLASS syntax. Example; -```JS + class MyComponent extends React.Component { constructor(props) { super(props); @@ -35,7 +33,6 @@ class MyComponent extends React.Component { ); } }; -``` The above creates an ES6 class MyComponent which extends the React.Component class. -Also notice the MyComponent class has a constructor defined within it that calls super(). It uses super() to call the constructor of the parent class, in this case React.Component. The constructor is a special method used during the initialization of objects that are created with the class keyword. It is best practice to call a component's constructor with super, and pass props to both. This makes sure the component is initialized properly. +Also notice the MyComponent class has a constructor defined within it that calls super(). It uses super() to call the constructor of the parent class, in this case React.Component. The constructor is a special method used during the initialization of objects that are created with the class keyword. It is best practice to call a component's constructor with super, and pass props to both. This makes sure the component is initialized properly. \ No newline at end of file diff --git a/react_tour/src/components/co2/Component2.js b/react_tour/src/components/co2/Component2.js index d4bbe5f..904fc1e 100644 --- a/react_tour/src/components/co2/Component2.js +++ b/react_tour/src/components/co2/Component2.js @@ -1,10 +1,10 @@ function Component2() { - return ( -
-

This is example of Function component

-
- ); - } - -export default Component2 \ No newline at end of file + return ( +
+

This is example of Function component

+
+ ); +} + +export default Component2; diff --git a/react_tour/src/exampleState/README.md b/react_tour/src/exampleState/README.md new file mode 100644 index 0000000..1bf66ed --- /dev/null +++ b/react_tour/src/exampleState/README.md @@ -0,0 +1,204 @@ +React components has a built-in state object. The state object is where you store property values that belongs to the component. +State consists of any data your application needs to know about, that can change over time. When the state object changes, the component re-renders. + +You create state in a React component by declaring a state property on the component class in its constructor. This initializes the component with state when it is created. The state property must be set to a JavaScript object. Declaring it looks like this: + +```JS +this.state = { + +} +``` + +You have access to the state object throughout the life of your component. You can update it, render it in your UI, and pass it as props to child components. The state object can be as complex or as simple as you need it to be. Note that you must create a class component by extending React.Component in order to create state like this. + +The state object is initialized in the constructor: +Class example + +```JS +class Car extends React.Component { + constructor(props) { + super(props); + this.state = { + brand: "Ford" + }; + } + render() { + return ( +
+

My Car

+
+ ); + } +} +``` + +Refer to the state object anywhere in the component by using the ( this.state.propertyname ) syntax + +```JS +class Car extends React.Component { + constructor(props) { + super(props); + this.state = { + brand: "Ford", + color: "red" + }; + } + render() { + return ( +
+

My Car {this.state.brand}

+

It is a {this.state.color} in color

+
+ ); + } +} +``` + +To CHANGE a value in the state object, use the this.setState() method +When a value in the state object changes, the component will re-render, meaning that the output will change according to the new value(s). + +Add a button that will change color; + +```JS +class Car extends React.Component { + constructor(props) { + super(props); + this.state = { + brand: "Ford", + color: "red" + }; + } + changeColor = () => { + this.setState({color: "blue"}); + } + render() { + return ( +
+

My Car {this.state.brand}

+

It is a {this.state.color} in color

+ +
+ ); + } +} +``` + +How to declare a new state in react using useState hook? Assuming we use functional components in this whole journey. +How to update state? +State accepts any type of data from strings, booleans, arrays, objects, numbers etc, how to update a single key in an object while leaving other keys unaffected? How to update array data( adding or removing an item in an array)? +Assume array of objects is passes as a state, how to update a single key in a single object of an array without affecting other keys of a particular object and without affecting other objects in the array? + +Hooks are a new addition in React 16.8. They let you use state and other React features without writing a class. + +//Function example + +```JS +import React, { useState } from 'react'; + +function Example() { + // Declare a new state variable, which we'll call "count" + const [count, setCount] = useState(0); + + return ( +
+

You clicked {count} times

+ +
+ ); +} +``` + +function components in React look like this: + +```JS +const Example = (props) => { + // You can use Hooks here! + return
; +} +``` + +or this + +```JS +function Example(props) { + // You can use Hooks here! + return
; +} +``` + +You might have previously known these as “stateless components”. We’re now introducing the ability to use React state from these, so we prefer the name “function components”. +Hooks don’t work inside classes. But you can use them instead of writing classes. + +The following example starts by importing the useState Hook from React: + +```JS +import React, { useState } from 'react'; + +function Example() { + // ... +} +``` + +What is a Hook? +A Hook is a special function that lets you “hook into” React features. For example, useState is a Hook that lets you add React state to function components. There ae other Hooks in React. + +When to use a Hooks +If you write a function component, and then you want to add some state to it, previously you do this by converting it to a class. But, now you can do it by using a Hook inside the existing function component + +Hooks are similar to JavaScript functions, but you need to follow these two rules when using them +(a)Do not call Hooks inside loops, conditions, or nested functions. Hooks should always be used at the top level of the React functions. This rule ensures that Hooks are called in the same order each time a components renders. +(b)You cannot call Hooks from regular JavaScript functions. Instead, you can call Hooks from React function components. Hooks can also be called from custom Hooks. + +Declaring a State Variable +In a function component, we have no this, so we can’t assign or read this.state. Instead, we call the useState Hook directly inside our component + +```JS +import React, { useState } from 'react'; + +function Example() { + // Declare a new state variable, which we'll call "count" + const [count, setCount] = useState(0); +``` + +We declare a state variable called count, and set it to 0. React will remember its current value between re-renders, and provide the most recent one to our function. If we want to update the current count, we can call setCount. + +Reading State: +In a function, we can use count directly: + +```JS +

You clicked {count} times

+``` + +Updating State: +In a function, we already have setCount and count as variables so we don’t need this: + +```JS + +``` + +Using Multiple State Variables + +Declaring state variables as a pair of [something, setSomething] is also handy because it lets us give different names to different state variables if we want to use more than one: + +```JS +function ExampleWithManyStates() { + // Declare multiple state variables! + const [age, setAge] = useState(42); + const [fruit, setFruit] = useState('banana'); + const [todos, setTodos] = useState([{ text: 'Learn Hooks' }]); +In the above component, we have age, fruit, and todos as local variables, and we can update them individually: + + function handleOrangeClick() { + // Similar to this.setState({ fruit: 'orange' }) + setFruit('orange'); + } +``` + +You don’t have to use many state variables. State variables can hold objects and arrays just fine, so you can still group related data together. However, unlike this.setState in a class, updating a state variable always replaces it instead of merging it. diff --git a/react_tour/src/exampleState/exampleState.js b/react_tour/src/exampleState/exampleState.js new file mode 100644 index 0000000..254bd6b --- /dev/null +++ b/react_tour/src/exampleState/exampleState.js @@ -0,0 +1,25 @@ +//We import the useState Hook from React. It lets us keep local state in a function component +import React, { useState } from "react"; +import "../Style/exampleState.css"; +function Example() { + /*Inside the Example component, we declare a new state variable + by calling the useState Hook. It returns a pair of values, + to which we give names. We’re calling our variable count + because it holds the number of button clicks. + We initialize it to zero by passing 0 as the only useState + argument. The second returned item is itself a function. + It lets us update the count so we’ll name it setCount.*/ + const [count, setCount] = useState(0); //here in square bracket we have used array destructuring + + return ( +
+

Examples on how to use "useState hook" in function component

+

You clicked {count} times

+ {/* When the user clicks, we call setCount with a new value. + React will then re-render the Example component, + passing the new count value to it.*/} + +
+ ); +} +export default Example; diff --git a/react_tour/src/exampleState/multipleState.js b/react_tour/src/exampleState/multipleState.js new file mode 100644 index 0000000..7e9f4de --- /dev/null +++ b/react_tour/src/exampleState/multipleState.js @@ -0,0 +1,28 @@ +import React, { useState } from "react"; +import "../Style/exampleState.css"; + +function ExampleWithManyStates() { + // Declare multiple state variables! + const [age, setAge] = useState(42); + const [fruit, setFruit] = useState("banana"); + const [todos, setTodos] = useState("Learn Hooks"); + + function handleOrangeClick() { + // Similar to this.setState({ fruit: 'orange' }) + setFruit("orange"); + } + return ( +
+

+ Assume array of objects is passes as a state, how to update a single key + in a single object of an array without affecting other keys of a + particular object and without affecting other objects in the array +

+

Age is {age}

+

My Todos is {todos}

+

This fruit is called {fruit}

+ +
+ ); +} +export default ExampleWithManyStates;