Challenge implies using anti-pattern which may have unexpected results in real situations
See original GitHub issueAffected page https://www.freecodecamp.org/learn/front-end-libraries/react/render-conditionally-from-props
The lines 242-246 of the file read:
handleClick() {
this.setState({
counter: 0 // Change this line
});
}
This implies that you can increment counter
as follows: counter: this.state.counter + 1
.
As already taught at this point of the curriculum, this method can result in unexpected behavior, since React may batch state updates for performance reasons.
AFAIK, the following is the correct way to handle this situation:
handleClick() {
this.setState(prevState => ({counter: prevState.counter + 1}))
}
Expected behavior
Right now, both methods of incrementing counter
are accepted. I suggest we change the provided code and the tests so that only the second method is implied and accepted.
System:
- Device: Laptop
- OS: Ubuntu 20.4
- Browser: Opera
- Version: 76
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (6 by maintainers)
Top Results From Across the Web
What is an Anti-Pattern? – BMC Software | Blogs
“An anti-pattern is a common response to a recurring problem that is usually ineffective and risks being highly counterproductive.” Note the ...
Read more >Software Testing Anti-patterns - Codepipes Blog
Almost always the history behind this anti-pattern involves developers who believe that unit tests have no real value and only integration tests ...
Read more >React course, incorrect usage of setState #39267 - GitHub
In the React lesson “Use State To Toggle An Element” ... using anti-pattern which may have unexpected results in real situations #42208.
Read more >Anti-Pattern Detection: Methods, Challenges, and Open Issues
Anti-patterns are poor solutions to recurring design problems. They occur in object-oriented systems when developers unwillingly introduce them while designing ...
Read more >4 DevOps anti-patterns that lead to disaster | TechBeacon
The problem arises when an anti-pattern becomes the new normal and the team stops there. The ability to recognize that you are using...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@suburban-daredevil There is already a PR for this which is awaiting requested changes. Let’s give them a chance to change what is needed first. If it does not happen within a reasonable timeframe we can use your PR instead.
Hi @soham283 thanks for reaching out and we appreciate your help. We would like you to start here: https://contribute.freecodecamp.org/#/FAQ
Happy contribuitng.