Skip to content

TodayILearned - 2023-06-19

Posted on:June 18, 2023 at 08:56 PM

#TIL

Notes taken from Quick Start - React

This is by no means a comprehensive #TIL of the react.dev’s quick start page. Rather, I took notes on parts that I wasn’t clear on or parts that I’ve learned for the first time.

The code that I’m using here is from the react.dev’s site; therefore, credit belongs to them, unless stated otherwise.

JSX:

return (
    <h1>
        {user.name}
    </h1>
); 
return (
  <img
    className="avatar"
    src={user.imageUrl}
  />
);
export const Profile() => {
  return (
    <section>
      <h1>{user.name}</h1>
      <img 
        className="profileAvatar"
        src={user.avatarUrl}
        alt={'Photo of ' + user.name}
        style={{
          width: 100px,
          height: 100px,
        }}
      />
   </section>
  );
}

Conditional rendering:

let content;
if (isLoggedIn) {
  content = <AdminPanel />;
} else {
  content = <LoginForm />;
}
return (
  <div>
    {content}
  </div>
); 

Hooks:

Sharing data between components

export default function MyApp() {
  const [count, setCount] = useState(0);

  function handleClick() {
    setCount(count + 1);
  }

  return (
    <div>
      <h1>Counters that update together</h1>
      <MyButton count={count} onClick={handleClick} />
      <MyButton count={count} onClick={handleClick} />
    </div>
  );
}