`values.map` 不是一个函数 reactjs.

huangapple go评论80阅读模式
英文:

values.map is not a function reactjs

问题

我正在从数据库中获取数据,想要将其显示在表格中,但最初我只想在浏览器中显示,而不是在表格中等等。但是我得到了一个错误 values.map is not a function,但我可以在控制台中看到值已经打印出来了。

这里是代码示例:

export default function SimpleTable() {
  const [values, setValues] = useState([]);

  // 获取数据的函数
  async function handleTable() {
    const res = await fetch("http://localhost:4000/productslist");
    const data = await res.json();
    setValues(data.data);
    console.log(data.data);
  }

  // 在组件加载时调用获取数据的函数
  useEffect(() => {
    handleTable();
  }, []);

  // 在浏览器中渲染数据
  return (
    <div>
      {console.log(values)}
      {values.map(v => {
        return (
          <h4 key={v.idaddproducts}>
            {v.productName} {v.productId} {v.productBrand}
          </h4>
        );
      })}
    </div>
  );
}

请注意,我将 useState 的初始值设置为一个空数组 [],因为您想要将数据映射到表格中,所以 values 应该是一个数组而不是对象。

英文:

i am fetchin details from database wanted to display that into the table, but for initial purpose i wanted to just display on browser without table and stuff.. am getting values.map is not a function but i could the see values printed in console

here iam using function component

export default function SimpleTable() {
const [values, setValues] = useState({});

here is the fetch function

async function handleTable(){
const res = await fetch(&quot;http://localhost:4000/productslist&quot;)
const data = await res.json()
setValues(data.data)
console.log(data.data)
}

calling the fetch function on useEffect

useEffect(()=&gt;{
handleTable()
},[])

Rendering the values into browser

return (
&lt;div&gt;

{console.log(values)}
{values.map(v =&gt; {
return  &lt;h4 key={v.idaddproducts}&gt;{v.productName}{v.productId}{v.productBrand}&lt;/h4&gt;})}
&lt;/div&gt;
);
}

here is the error

Uncaught TypeError: values.map is not a function

答案1

得分: 3

The initial state of your values is an empty object

From

const [values, setValues] = useState({});

Update to

const [values, setValues] = useState([]);

Since Object doesn't have a method called .map

Also in your code since it's an async call, you can check based on a state whether data has loaded or not. A simple thing you can add is a ternary check:

{values.length && values.map(v => {
  return <h4 key={v.idaddproducts}>{v.productName}{v.productId}{v.productBrand}</h4>;
})}
</div>
);

So once the array has a length greater than zero, only then it will execute the map function.

Working Codesandbox With Sample Example

英文:

The initial state of your values is an empty object

From

const [values, setValues] = useState({});

Update to

const [values, setValues] = useState([]);

Since Object doesn't have a method called .map

Also in your code since its a async call you can check based on a state whether data has load or not a simple thing you can add is a ternary check

{values.length &amp;&amp; values.map(v =&gt; {
return  &lt;h4 key={v.idaddproducts}&gt;{v.productName}{v.productId}{v.productBrand}&lt;/h4&gt;})}
&lt;/div&gt;
);

so once the array has a length greater than zero only it will execute the map function

Working Codesandbox With Sample Example

答案2

得分: 1

将您的状态定义为一个数组

const [values, setValues] = useState([]);

代码

{values && values.map(v => {
  return (
    <h4 key={v.idaddproducts}>
      {v.productName}{v.productId}{v.productBrand}
    </h4>
  );
})}
英文:

Define your state as an array

const [values, setValues] = useState([]);

Code

{values &amp;&amp; values.map(v =&gt; {
  return  &lt;h4 key={v.idaddproducts}&gt;
           {v.productName}{v.productId}{v.productBrand} 
          &lt;/h4&gt;
  })
 }

答案3

得分: 0

你设置了一个值{}而不是[]
更新

const [values, setValues] = useState({});

const [values, setValues] = useState([]);
英文:

You set a value {} instead of [].
Update

const [values, setValues] = useState({});

to

const [values, setValues] = useState([]);

huangapple
  • 本文由 发表于 2020年1月6日 14:48:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/59607803.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定