A
A
Alekosh Akhosh2021-08-05 22:26:21
React
Alekosh Akhosh, 2021-08-05 22:26:21

How to add a state to check the ban?

How to add another state that will show if the user is banned or not. Display information about it in some tag?

import React from 'react';

function App() {
    let [user, setUser] = React.useState();

    return <div>
        <span>{user}</span>
        <button onClick={() => setUser('Alekosh')}>btn1</button>
        <button onClick={() => setUser('Ambrose')}>btn2</button>
    </div>
}

export default App;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alice, 2021-08-05
@Akhosh

import React, { useState } from "react";

const App = () => {
  /** Так */
  const [user, setUser] = useState({
    username: "lol",
    isBanned: false,
  });

  /** Или отдельным, так */
   const [user, setUser] = useState({
     username: "lol"
   });
  const [isBanned, setBanned] = useState(false);

  return (
    <>
      {/** В первом случае */}
      <span>{user.isBanned ? "Забанен" : "Бана нет"}</span>
      <button onClick={() => setUser({ username: "Alekosh", isBanned: false })}>
        btn1
      </button>
      <button onClick={() => setUser({ username: "Ambrose", isBanned: true })}>
        btn2
      </button>

      {/** Во втором случае */}
      <span>{isBanned ? "Забанен" : "Бана нет"}</span>
      <button onClick={() => setUser({ username: "Alekosh" })}>
        btn1
      </button>
      <button onClick={() => setUser({ username: "Ambrose" })}>
        btn2
      </button>
    </>
  );
};

export default App;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question