The useState hook in Next.js with Typescript is not supported

I'm working on adding a filter to my movie list. My plan is to store all the movies in 'default' and then apply filters from there when needed. However, I encountered an error along the way:

Here's a snippet of my code:

 const [movies, setMovies] = useState<string[]>([])
    const [loading, setLoading] = useState(false)
    const [defaultMovies, setDefaultMovies] = useState([])


  useEffect(() => {
    const fetchData = async() => {
      setLoading(true)
      await fetch("https://seleksi-sea-2023.vercel.app/api/movies")
      .then(res => res.json())
      .then(data => {
        setMovies(data)
        setDefaultMovies(data)
        setLoading(false)
      })
      
    };

    fetchData()
  }, []);

  const getCategory = async(category: string) => {
    console.log(category)
    if(category === 'Kids'){
        const filteredMovies = defaultMovies.filter(data => data.age_rating <= 12)
        setMovies(filteredMovies)
    }
    else if(category === 'Adults'){
        const filteredMovies = defaultMovies.filter(data => data.age_rating > 12)
        setMovies(filteredMovies)
    }
  }

I've been able to filter my movies successfully, but I'm open to any suggestions or advice for improving this approach.

Answer №1

There is an issue with the useState function. The term default is a reserved word in Javascript, so it needs to be changed to something else.

Source

JavaScript has several keywords with special meanings such as: break, case, catch, continue, debugger, default, delete, do, else, finally, for, function, if, in, instanceof, new, return, switch, this, throw, try, typeof, var, void, while, and with.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Updating NavBar text dynamically based on user login status in React.JS

Within my project, there is a Navigation bar that I access from App.js. Depending on whether I am logged in or not, I want to display different versions of the NavBar. When logged in, the NavBar should have a logout button. And when logged out, it should s ...

Renderer's Delayed Electron Loading

I am in the process of developing a Electron application using TypeScript and React. This application serves as an account manager, allowing users to retrieve and view data for specific accounts. However, I have encountered an issue with the ipcMain.on(&a ...

What is the method for executing a nested query in the Parse API?

Within the user object, there is a list of features: skills: {"piano:10", "singing:5", ....}; I am looking to filter users based on their 'piano' skills. How can I achieve this? It doesn't have to be an exact match but should follow the ru ...

creating intricate services using ngTagsInput

Integrating ngTagsInput into my blog allows users to add existing or custom tags to new posts. The blog utilizes a firebase datasource accessible through a factory: servicesModule.factory("postsDB", function($resource){ return $resource("https://data ...

Could use some help with configuring express routes with parameters

Greetings! I am new to working with Express and I am currently in the process of creating a portfolio website. In my project, I have a Pug file named project.pug which includes HTML content that should dynamically render based on the ID of each project sto ...

Dynamic JavaScript button control based on time

I am currently working on an app and my goal is to have a feature where the user clicks a button, it will then disappear and only reappear after 24 hours. Here's my progress so far. <div class="buttons"> <p><button onclick="hide(); ...

A guide to replacing or customizing standard components in ReactJS

I need to modify the color property of a common component so I can use it elsewhere. const ViewAllIcon = (props) => ( <svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 26 ...

Utilize CSS to ensure that images are equal in height to their container element

This is the markup div.shadow | div#content |img.shadow | Is there a way to ensure that the shadow images always maintain the same height as the content area? The challenge lies in the fact that the content area can adjust its size based on different fac ...

jQuery tab plugin does not open in a new browser tab when the 'ctrl' key is pressed

I have implemented the Jquery easy tab plugin on my webpage. When I perform a right-click on each tab and open it in a new browser tab, it displays correctly. However, if I press the ctrl key on the keyboard and click on a tab, it opens in the same browse ...

What could be improved in this Angular script?

angular.module('events.services', []) .factory('EventService', function($http, $cordovaSQLite) { return { fetchData: function() { return 'Data fetched successfully!'; } ...

Stopping the recursive function call in AngularJS

Utilizing the following function to retrieve users from a REST API, paginated by offset. Upon successful callback, the function recursively calls itself with a new offset to fetch the next set of users. Issue: When I change or exit the view, the FetchAtte ...

Unforeseen Extra Information is being transmitted along with Socket.io and Jquery

Issue : There seems to be a problem with the chat message printing an extra character in msg. Upon console logging, an additional object value is appearing in the state. What could be causing this issue? Console log output : 2{"message":"e"} 1{"mess ...

Using HTML5 Canvas: Implementing an Image.onload() function through Image.prototype

I am trying to create a simple function that will refresh the canvas automatically when an image is loaded. The idea is to be able to use code like this: var map = new Image(); map.onloadDraw(); map.src = "images/" + worldmapCanvasShapeImage; Currently, ...

What is the best way to access the userPass value from an array of objects in Angular 7?

How can I retrieve the userPass value from an array of objects using Angular 7? I am looking to access the userPass property value from an array of objects. I have a variable named auth of type any. The auth variable contains an array of objects. I wan ...

Is there a way to incorporate a loading spinner into a MaterialUI DataTable without having to specify a fixed height for the parent component?

Currently, I am using a MaterialUI DataTable with the following setup: <div style = {{height: 300}}> <DataGrid loading={true} getRowHeight={() => "auto"} getEstimatedRowHeight={() => 250} ...

Using a ternary operator to render a span tag in ReactJS

I need to display a number in a span tag with larger font size in Spanish. Here is my React.js code using a ternary operator: <div> {togo !== 0 ? (<div className="text-center"><span className="display-4">{togo}</span>{togo > ...

When using Vue.js, binding may not function properly if it is updated using jQuery

Link to JsFiddle Below is the HTML code: <div id="testVue"> <input id="test" v-model="testModel"/> <button @click="clickMe()">Click me</button> <button @click="showValue()">Show value</button> </div& ...

Can JQuery be used to identify the CSS styles applied to selected elements?

Highlight the text by selecting it and clicking a button. If the text is already highlighted, clicking the button should make the selected text return to normal. Can this be achieved using jQuery and a single button? Is it possible to identify the CSS st ...

Is jQuery Autocomplete functioning properly on outdated browsers, but not on newer ones?

Here is the JSON data I have for my auto complete feature { "list" : [ { "genericIndicatorId" : 100, "isActive" : false, "maxValue" : null, "minValue" : null, "modificationDate" : 1283904000000, "monotone" : 1, "name":"Abbau", ...

What are the steps for designing personalized syncfusion grid-columns while still preserving the built-in search functionality of syncfusion?

Looking to transform the data coming from the backend, specifically mapping a user's status which is represented as a number to its corresponding string value. Considered using typescript for this mapping task, but it interferes with syncfusion' ...