Jake Chasan
Jake Chasan

Reputation: 6560

React: Pass Function with JSON Data

I am trying to generate components from a JSON file with React-Native, and am wondering what is the best way to pass a function to a component as a prop?

Example, button requires the prop onPress, so is there a way to pass in a function to the object onPress as I have done here:

{
      component: "Button",
      props: {
            title: "This is the Title",
            onPress: {() => {Alert.alert("Tapped!")}}
      }
}

As this generates an unexpected token error, what is a better way to accomplish this?

Upvotes: 1

Views: 3246

Answers (1)

Ross Allen
Ross Allen

Reputation: 44900

You have extra curly brackets around the function declaration that are invalid. Remove those, and you should be good:

{
      component: "Button",
      props: {
            title: "This is the Title",
            onPress: () => {Alert.alert("Tapped!")}
      }
}

Upvotes: 3

Related Questions