Skip to Main Content
TWIL
Python
React

Welcome to this week's TWIL journey, where continuous growth is part of our DNA! Emily provides two insightful snippets to enhance your tech toolkit. First up, learn the quirks of Truthy Dictionary Keys and how Python determines the unique keys in a dictionary - a crucial piece of information for any Pythonista. Then, advance your understanding of React's setState method with SetState Argument for Previous State, grasping the use of prevState for more predictable state transitions. Whether it's a pythonic puzzle or a React revelation, these micro-learnings pave the way for smarter coding.

Truthy Dictionary Keys

Overriding:

>>> d = {'a': 0, 1: 'one', True: "true"}
>>> d
{'a': 0, 1: 'true'}

Accessing:

>>> d = {'a': 0, 1: 'one'}
>>> d
{'a': 0, 1: 'one'}
>>> d[True]
'one'

  • Python
Emily Morehouse's profile picture
Emily Morehouse

Cofounder, Director of Engineering


SetState Argument for Previous State

Previously, we would do something like:

const { num } = this.state
this.setState({
  num: num + 1,
})

setState can also take a function instead of an object, which provides us with the previous state as an argument:

this.setState(prevState => ({
  num: prevState.num + 1,
}))

// With parameter destruction
this.setState(({ num: prevNum }) => ({
  num: prevNum + 1,
}))

  • React
Emily Morehouse's profile picture
Emily Morehouse

Cofounder, Director of Engineering

Related Posts

Katie demonstrating RSpec matchers in Ruby and Rails during the "This Week I Learned" series to improve testing techniques.
September 28, 2022 • Frank Valcarcel

TWIL 2022-09-23

Dive into this week’s TWIL and uncover the secrets of RSpec matchers in Ruby and Rails, as Katie guides us through enhancing our testing practices for better precision and resilience.

Featured image for TWIL blog post on Git aliases, showing a simplified illustration of branch management and code snippets.
June 1, 2022 • Frank Valcarcel

TWIL 2022-05-20

Dive into this week’s TWIL for a quick learning session on Git aliases with Katie, and discover how to enhance your workflow efficiency with effective shortcut commands for branch management.