0

I'm new to React.

I have the code below with a function, but when I run it, it returns an error:

TypeError: renderJson[item.node] is not a function.

How can I fix the renderJson function?

export const readItem = item => {
  printlog(item);

  return renderJson[item.node](item);
};

const renderJson = {
  "heading": item => <h1>{item.map(item => readItem(item))}</h1>
};

nopassport1
  • 1,821
  • 1
  • 25
  • 53
ccd
  • 23
  • 2
  • Your `renderJson` is not a function. It's not accepting any parameters. You've declared `renderJson` as a variable – nopassport1 Jan 24 '20 at 15:11
  • can you give some additional information that could help. What exactly do you want to do? – Ropo Jan 25 '20 at 01:17

1 Answers1

0

If you're trying to create a single React functional component that takes a JSON, and outputs the items in the JSON as a header, it would be more like this:

// If you're getting this JSON from an external source using something like a GET request, put the request inside a "useEffect()" hook

const myJson = {
       "heading": ["My First Header", "My Second Header"] 
};

export const Header = () => {

  console.log(myJson);

  return <h1>{myJson.heading.map(header => header}</h1>

};

I apologize if this is a misinterpretation of your question. If it is, any additional details would be helpful.

Jay
  • 41
  • 6