简体   繁体   English

在 React 中渲染数组

[英]Rendering an array in React

I'm trying to render my arr in my render function.The code doesn't give any error.Here I'm trying to calculate the fibonacci series and display it on the screen.The problem with the code is when I enter a certain number in the text field it doesn't print the series on the screen but when I clear the text field without refreshing it prints the correct series on the screen.I can't understand where this error comes from.我试图在我的渲染函数中渲染我的arr 。代码没有给出任何错误。在这里我试图计算斐波那契数列并将其显示在屏幕上。代码的问题是当我输入某个文本字段中的数字不会在屏幕上打印系列,但是当我清除文本字段而不刷新时,它会在屏幕上打印正确的系列。我无法理解此错误的来源。

import React, { useState } from 'react';
import { Button, TextField } from '@material-ui/core';
import './App.css';

function App() {
  const [limit, setLimit] = useState(1);
  const [arr, setArr] = useState([]);

  function handleGenerateFibonacci(event) {
    for (const n of generator(limit)) {
      arr.push(n)
      setArr(arr)
    }
    event.preventDefault();
  }

  function* generator(limit) {
    var i = 0;
    var a = 0;
    var b = 1;
    yield a;
    yield b;
    for (i = 1; i <= limit - 1; i++) {
      var fibNumber = a + b
      yield fibNumber
      a = b
      b = fibNumber
    }
  }



  return (
    <div className="App">
      <form >
        <h1>Generating the Fibonacci Series</h1>
        <h2>Enter the range</h2>
        <TextField onChange={e => setLimit(e.target.value)}></TextField>
        <br />
        <Button onClick={handleGenerateFibonacci} type="submit" style={{
          height: 40,
          width: 160,
          borderRadius: 10,
          backgroundColor: "Blue",
          marginLeft: 50,
          marginRight: 50,
          marginTop: 20,
          fontSize: 16,
          color: "white"
        }} >Calculate</Button>
        <br />
        <h2>Result:</h2>
        <div >
        <p>Fibonacci Series : {"[ " + arr + " ]"}</p>
        </div>
      </form>
    </div>
    
  );
}


export default App;

Because when you click button your code doesn't change value itself, just content of value因为当您单击按钮时,您的代码不会改变值本身,只是值的内容

function handleGenerateFibonacci(event) {
    for (const n of generator(limit)) {
      arr.push(n)
      setArr(arr)
    }
    event.preventDefault();
  }

...so system doesn't really know that your array has been changed, so better write it this way ...所以系统并不真正知道您的数组已被更改,所以最好这样写

function handleGenerateFibonacci(event) {
    for (const n of generator(limit)) {
      arr.push(n)
    }
    setArr([...arr])
    event.preventDefault();
  }

PS when you clean the text field, limit property of state is correctly changing, so system refresh the components PS 当您清理文本字段时,状态的限制属性正确更改,因此系统刷新组件

Try this approach,试试这个方法,

function handleGenerateFibonacci(event) {
    for (const n of generator(limit)) {
      setArr((a) => [...a, n]);
    }
    event.preventDefault();
  }

working demo - https://codesandbox.io/s/festive-mayer-xu91t?file=/src/App.js:270-423工作演示 - https://codesandbox.io/s/festive-mayer-xu91t?file=/src/App.js:270-423

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM