Is it recommended to use state for static variables?

I’m working on a next.js app and I have two pages that have the same code. Here’s a snippet from pages/books/[cid]

export default function Book() {
  const [sourceType] = useState("book")
  useEffect(() => {
    dispatch(changeActiveSource(`${sourceType}s`));
    dispatch(
      fetchFullData({ url: `http://localhost:1338/${sourceType}-series`, 
                    type: `${sourceType}s` })
    );
  }, []);
}

Note const [sourceType] = useState("book") I’m not using setSourceType is that ok?

And I was thinking of duplicating that page in pages/courses/[cid]. I know about templates (but this is not a template). Is it recommended to duplicate the page or is there a pattern to follow for this?

I’m relatively new to react, but can’t see any point in using a useState hook without a setter.

Given that ‘book’ isn’t being passed in as an argument to the function, in this instance why not just keep it simple?

dispatch(changeActiveSource('books'))
dispatch(
  fetchFullData({ 
      url: 'http://localhost:1338/book-series', 
      type: 'books' 
  })
)

If the function did expect a sourceType argument, then the templates would make sense to me

export default function getSeries(sourceType) {

  useEffect(() => {
    dispatch(changeActiveSource(`${sourceType}s`))
    dispatch(
      fetchFullData({ 
        url: `http://localhost:1338/${sourceType}-series`, 
        type: `${sourceType}s` })
    )
  }, [])
}

I’m using it all over the place, I just didn’t feel the need to post the entire code.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.