react basics code example

Example 1: React js tutorial

npx create-react-app my-app
cd my-app
cd src

# If you're using a Mac or Linux:
rm -f *

# Or, if you're on Windows:
del *

# Then, switch back to the project folder
cd ..

Example 2: reactjs basic example

// -------// -------// -------
// index.js
// -------// -------// -------

const user = {
  name: "zidane",
  email: '[email protected]'
};

const element = <h1> {user.name} exist email: {user.email} </h1>;

const element2 = 
  <div  className="heading"> 
     <h1> {user.name}  </h1>
     <h1> {user.email} </h1>
  </div>;

var numbers = [1,2,3,4];
var doubleNumbers = numbers.map(function(num){
  return num * 2 + "; ";
})

var doubleNumberArrowFunction = numbers.map((num)  => num * 3 + "; ");

var element3 = 
  <div>
    Double numbers: {doubleNumberArrowFunction}
  </div>;

ReactDOM.render(
  element3,
  // <React.StrictMode>
  //   <App />
  // </React.StrictMode>,
  document.getElementById('root')
);

// -------// -------// -------
// ------- index.css ------
// -------// -------// -------
.heading {
  padding: 10px;
  color: white;
  background-color: green;
}

Example 3: react quick tutorial

ReactDOM.render(  
    <Hello />,   
    document.getElementById("root")  
);

Example 4: create tic tac toe game in react using jsx files

useEffect(()=>{
        //checking winner row and col
        for (let i = 0; i <= 2; i++){
            const idx = (i % 3) * 3 // -> 0,3,6
            //check row
            if ( (table[idx] + table[idx+1] + table[idx+2] )=== 9 || (table[idx] + table[idx+1] + table[idx+2] ) === 15){
                setWinner([idx,idx+1,idx+2])
                gameOver()
            }
            //check col
            if ((table[i] + table[i+3] + table[i+6] )=== 9 || (table[i] + table[i+3] + table[i+6] ) === 15){
                setWinner([i,i+3,i+6])
                gameOver()
            }
        }
        //checking winner diagonal
        if ((table[0] + table[4] + table[8] ) === 15 || (table[0] + table[4] + table[8] ) === 9 ){
            setWinner([0, 4, 8])
            gameOver()
        }
        if ((table[2] + table[4] + table[6] ) === 9 || (table[2] + table[4] + table[6] ) ===15){
            setWinner([2, 4, 6])
            gameOver()
        }
        // check if table completed
        if (table.indexOf(0) === -1){
            gameOver()
        }
    }, [table])