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

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.

March 19, 2018 • Frank Valcarcel

Google Developer Group – Women Techmakers Lightning Talks

At Women Techmakers Day, hosted by the Google Developer Group in Boulder, Emily shares a deeply personal and insightful perspective on managing exhaustion and finding fulfillment.