Skip to content
Advertisement

how to pass an object as a prop in nextjs

I cant quite figure out how I am supposed to pass an object as a prop when using useState in Next JS.

I have a lorem ipsum generator that I created in javascript functions. I have a component called Paragraphs that houses it. I need to pass in two properties,

  1. a number of paragraphs.
  2. a sentence length.

The paragraph length is set by a text input where the user types in 1-10. The sentence length is set by radio buttons.

The problem I am running into is that when you input any value, the setState gets called (intentional) and it works, the problem is, it constantly works. I want to only have it update when I click my “Adventure” button to generate the data. I am unsure how to set those values to an set them as object property values and pass the object then.

Below is my code for the fields

JavaScript

Advertisement

Answer

What I’d do is refactor the input functionality into a separate component and use a function prop to pass the input data to an outer component that also contains the Paragraph component, like so:

JavaScript

That way, settings in rpgIpsum is only updated when the button inside ParagraphInput is pressed, and not on every change of the inputs.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement