why function in react app will not render


The code runs without errors on the console.Even after i write the if block no error is in the console,but the react app wont display any JSX ,just blank page

my code works when you remove the if(} block

import { useState } from "react";
const Zoom = () => {
  const [btnable, setbtnable] = useState(true);
  const [stoke, setStoke] = useState("");
    if (stoke === "") {
    } else {

  return (
    <div className="container">
      <br />
        onChange={(e) => {
      <button disabled={btnable}>Zoom</button>

export default Zoom;

>Solution :

The issue is an infinite render.

When you update component state, you cause that component to rerender.

Every time the component rerenders, the if else block is being run again. Which is calling set state again. Which is causing the component to rerender, and calling the if else again, and setting state again, rendering again and…. you get the point. It’s an infinite loop.

When you’re doing this upfront logic that causes state to change, you should wrap it in a useEffect – that way you won’t get stuck in an infinite loop of rerendering on state changes.

Your code is confusing me and you didn’t explain what you want the behavior to be. But my guess is you want this, or at least something like it:

const [btnable, setBtnable] = useState(true);
const [stoke, setStoke] = useState("");

useEffect(() => {
   if (stoke === "") {
   } else {
}, [stoke]);

Read up on component rendering, state changes, useState and useEffect hooks. Seems like you’re missing some key insights which is causing your confusion here. Best of luck.

Leave a ReplyCancel reply