Example parent:
// parent.js
return <Child complexValue={{a: 1, b:2}} simpleValue={12}/>
My child is starting to use props.complexValue
and props.simpleValue
30+ times in 30+ functions. I'm wondering what are the best practices so that if either of them change down the road, I don't have to re-update / change logic to work in so many places. It's also nice to not see so much props.*
spread out everywhere in my child code.
What's the best practice to handle this situation? I'm considering Option 1 and 2 vs Option 3, but don't understand React very well so would appreciate any tips / pointers / gotchas / resource links I can read up on. Wasn't sure what this kind of concept was called in React.
Option 1 - Declare function to return props value
// child.js
const Child = (props) => {
const getComplexValue = () => {
return props.complexValue
}
const getSimpleValue = () => {
return props.simpleValue
}
const fnA = () => {
const simpleValue = getSimpleValue()
if (simpleValue == 12) {
return true
}
}
const fnB = () => {
const complexValue = getComplexValue()
if (complexValue == 12) {
return true
}
}
// lots more similar functions declarations
...
}
Option 2 - Declare variable pointing to props value
// child.js
const Child = (props) => {
const complexValue = props.complexValue
const simpleValue = props.simpleValue
const fnA = () => {
if (simpleValue == 12) {
return true
}
}
const fnB = () => {
if (complexValue == 12) {
return true
}
}
// lots more similar functions declarations
...
Option 3 - Directly use props everywhere
// child.js
const Child = (props) => {
const fnA = () => {
if (props.simpleValue == 12) {
return true
}
}
const fnB = () => {
if (props.complexValue == 12) {
return true
}
}
// lots more similar functions declarations
...