最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React.js useState with Array.push method on functional component - Stack Overflow

programmeradmin1浏览0评论

I want to change value of state with useState hook, but I've never worked with useState hook in functional component before, and I get a very strange error. Even though I define all of my variables as string[], it tries to define whole array as number.

import * as React from 'react'
import { NextPage } from 'next'

const IndexPage: NextPage = () => {
  const [countries, setCountries] = React.useState<string[]>([])

  return (
    <>
      {
        ["Dallas", "Oregon", "New York", "California"].map((x: string) =>
          <button onClick={() => setCountries(countries.push(x))}>
            <span>{x}</span>                  ^^^^^^^^^^^^^^^^^
          </button>
        )
      }
    <>
  )
}

Here is strange error I get:

(method) Array<string>.push(...items: string[]): number
"Argument of type 'number' is not assignable to parameter of type 'SetStateAction<string[]>'.ts(2345)"

I want to change value of state with useState hook, but I've never worked with useState hook in functional component before, and I get a very strange error. Even though I define all of my variables as string[], it tries to define whole array as number.

import * as React from 'react'
import { NextPage } from 'next'

const IndexPage: NextPage = () => {
  const [countries, setCountries] = React.useState<string[]>([])

  return (
    <>
      {
        ["Dallas", "Oregon", "New York", "California"].map((x: string) =>
          <button onClick={() => setCountries(countries.push(x))}>
            <span>{x}</span>                  ^^^^^^^^^^^^^^^^^
          </button>
        )
      }
    <>
  )
}

Here is strange error I get:

(method) Array<string>.push(...items: string[]): number
"Argument of type 'number' is not assignable to parameter of type 'SetStateAction<string[]>'.ts(2345)"
Share Improve this question edited Sep 9, 2019 at 16:15 norbitrial 15.2k10 gold badges38 silver badges64 bronze badges asked Sep 9, 2019 at 15:19 Abdulsamet KurtAbdulsamet Kurt 1,1412 gold badges11 silver badges16 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 12

Array.push returns a number so you have to change your code to this

import * as React from 'react'
import { NextPage } from 'next'

const IndexPage: NextPage = () => {
  const [countries, setCountries] = React.useState<string[]>([])

  return (
    <>
      {
        ["Dallas", "Oregon", "New York", "California"].map((x: string) =>
          <button onClick={() => {
          // either this
          setCountries(countries.concat(x))
          // or
          setCountries([...countries, x]
          }>
            <span>{x}</span>                  
          </button>
        )
      }
    <>
  )
}

you need to pass an array of strings to setCountries

<button onClick={() => setCountries([...countries, x])}>

Array.push returns number :

The new length property of the object upon which the method was called.

Also you probably don't want to mutate the state, try setCountries(countries.concat(x))

In your case, setCountries expects that you pass an array of string items, but what is being passed to it is the return type of the function Array.push which is of type number

If you want to just remove a country on clicking that button, you can do so using -

return (
    <>
      {
        ["Dallas", "Oregon", "New York", "California"].map((x: string) =>
          <button onClick={() => setCountries(countries.concat(x))}>
            <span>{x}</span>                  
          </button>
        )
      }
    <>
  )
发布评论

评论列表(0)

  1. 暂无评论