Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
380 views
in Technique[技术] by (71.8m points)

reactjs - Getting this error while my timer is ending: "Can't perform a React state update on an unmounted component. This is a no-op, but it indicates..."

So I thing this is happening because of the conditional rendering and state change. Here is my rendering:

!isOn
    ?
        {...}
    :
        <CountDown
            until={2}
            onFinish={onFinish}
            digitStyle={styles.timerContainer}
            digitTxtStyle={styles.timerTextSize}
            timeToShow={mins >= 60 ? ["H", "M", "S"] : ["M", "S"]}
            timeLabels={{}}
            separatorStyle={styles.timerTextSize}
            showSeparator
        />

Here is my onFinished method:

const onFinish = () => {
    setTimeHook({
        ...timeHook,
        isOn: 0,
    });
};

And the state:

const time = {
    mins: 5,
    isOn: 0,
};

const [timeHook, setTimeHook] = useState(time);

I'm also not familiar about what does even mean this error, so any info would be great. Though the code works well, I'm afraid of memory leak. So how can I fix that problem?

question from:https://stackoverflow.com/questions/65942839/getting-this-error-while-my-timer-is-ending-cant-perform-a-react-state-update

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes, It's cause of conditional rendering. Components is cleared completely in this case.

You can try display prop to show/hide component display?: "flex" | "none".

Here's an example

<View style={{ display: !isOn? 'flex' : 'none' }}>
  {...}
</View>
<View style={{ display: isOn? 'flex' : 'none' }}>
<CountDown
   until={2}
   onFinish={onFinish}
   digitStyle={styles.timerContainer}
   digitTxtStyle={styles.timerTextSize}
   timeToShow={mins >= 60 ? ["H", "M", "S"] : ["M", "S"]}
   timeLabels={{}}
   separatorStyle={styles.timerTextSize}
   showSeparator
/>
</View>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...