December 17, 2018
we have a bunch of stuff.
const aValue = {
data: 42,
happyPlace: 'tennoji temple in Kyoto',
}
we want to pass it into our functions to use (ES6). here are three ways to do it. note that method 3 (cFunction) will give you the variable right then and there, without needing you to call the input (props) and assign it to a variable.
const aFunction = props => {
const { data } = props
return data // 42
}
const bFunction = props => {
const data = props.data
return data // 42
}
const cFunction = ({ data }) => { // i only want to access the value at the key 'data' from
// the input.
return data // 42
}
and how do you access multiple properties from the object with method 3?
const cFunctionAgain = ({ data, happyPlace }) => {
console.log(data)
console.log('my favourite place in the world is ' + happyPlace)
return data // 42
}
https://hacks.mozilla.org/2015/05/es6-in-depth-destructuring/