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

Image for 'This Week I Learned' blog post featuring tips on managing Git branches including renaming and syncing with remote changes.
October 8, 2019 • Frank Valcarcel

TWIL 2019-10-04

Join us for ‘TWIL,’ where Emily shares innovative Git tips for managing branches with rewritten history. Learn to sync local branches with remote changes and rename them effectively.

The PyCon 2018 logo, where Cuttlesoft Co-founder, Emily Morehouse delivered a talk about abstract syntax trees in CPython.
May 12, 2018 • Frank Valcarcel

Emily at PyCon 2018 – “The AST and Me”

Get under the hood and learn about Python’s beloved Abstract Syntax Tree with Cuttlesoft’s very own, Emily Morehouse.