I am trying to access an element of a prop. I can access the element with a console.log
, but not in a return
.
function Viewer(props) {
// Step 1: Viewer receives a prop called "json".
const Footer = (props) => {
console.log(props.json.lines[1]) // Step 3: This successfully logs the second element of the array.
return (
{ props.json.lines[1] } // Step 4: "TypeError: props.json is undefined" (but it's not)
)
}
// Step 2: Viewer calls Footer, passing prop.json to Footer.
return (
<main>
<Footer
json={props.json}
/>
</main>
)
}
This code seems quite straightforward, but doesn't work. Even though console.log
ing the element words, trying to return the element yields TypeError: props.json is undefined
.