简体   繁体   English

在 React 中存储数据

[英]Storing data in React

I'm actually new to React, and can't choose, what is the best way to store data in a situation like this: I have a form with some inputs, and I need to do some actions with all data from these inputs on submit.我实际上是 React 的新手,无法选择在这种情况下存储数据的最佳方式是什么:我有一个包含一些输入的表单,我需要对这些输入中的所有数据执行一些操作提交。 All inputs are stored in one Component.所有输入都存储在一个组件中。 So, I need to get all the data only on submit.所以,我只需要在提交时获取所有数据。 And now I'm trying to choose the best way to store this data.现在我正在尝试选择存储这些数据的最佳方式。 I see 2 ways:我看到两种方式:

  • Storing data in the state.在状态中存储数据。 But as React Docs describes:但正如 React Docs 描述的那样:

    "Only data which need to render may store in the state." “只有需要渲染的数据才能存储在状态中。”

    But I don't need this data for render, I need to work with this only on submit.但是我不需要这些数据进行渲染,我只需要在提交时使用它。

  • Storing as class variables.存储为类变量。 It looks good for me, because when i using state, i need to call setState(), which trigger render(which i don't need), or this.state.data = ....But React Docs says that:这对我来说看起来不错,因为当我使用状态时,我需要调用 setState(),它会触发渲染(我不需要),或者 this.state.data = ....但是 React Docs 说:

    "You may change state directly only in constructor." “您只能在构造函数中直接更改状态。”

So, which of these ways are better and why?那么,这些方法中哪一种更好,为什么?

I think you're overthinking it, just stick with controlled components and manage your form data through state.我认为你想多了,只是坚持使用受控组件并通过状态管理你的表单数据。

However, if you really don't want to use controlled components because you don't want the render method to be called then you don't have to.但是,如果您真的不想使用受控组件,因为您不想调用 render 方法,那么您不必这样做。

This is because form elements work a little bit differently from other DOM elements in React, because in HTML, form elements such as <input> , <textarea> , and <select> naturally maintain their own state and update it based on user input.这是因为表单元素的工作方式与 React 中的其他 DOM 元素略有不同,因为在 HTML 中,诸如<input><textarea><select>类的表单元素自然会维护自己的状态并根据用户输入进行更新。 See official forms docs .请参阅官方表格文档

And React doesn't take that away from you.而 React 不会让你失去这些。 This means that you can make use of forms the same way you would with vanilla JS.这意味着您可以像使用 vanilla JS 一样使用表单。

Also worth mentioning that in React world this way of leaving data to be purely handled by the DOM rather than handling it with a React Component is known as Uncontrolled Component .还值得一提的是,在 React 世界中,这种将数据完全由 DOM 处理而不是使用 React 组件处理的方式被称为不受控制的组件

Implementation实施

As far as how this would look in practise, I can think of two ways that you can do this, one would be with refs :至于这在实践中的样子,我可以想到两种方法可以做到这一点,一种是使用refs

handleSubmit = (e) => {
  e.preventDefault();
  console.log(this.input.value); // whatever you typed into the input
}

render() {
  return (
    <form onSubmit={this.handleSubmit}>
      <input type="text" name="name" ref={input => this.input = input} />
      <input type="submit" value="submit" />
    </form>
  );
}

Another way would be through an event object:另一种方法是通过event对象:

handleSubmit = (e) => {
  e.preventDefault();
  console.log(e.target.name.value); // whatever you typed into the input
}

render() {
  return (
    <form onSubmit={this.handleSubmit}>
      <input type="text" name="name" />
      <input type="submit" value="submit" />
    </form>
  );
}

For storing formularies I would definitely choose to store data in the state, if you look at the React form official documentation they explain what I think is your case.对于存储公式,我肯定会选择将数据存储在状态中,如果您查看React 表单官方文档,它们会解释我认为您的情况。

The standard you will see the most, as far as I know, is this:据我所知,你最常看到的标准是:

class Form extends Component {
    constructor (props) {
    super(props)
    this.state = {
      name: '',
      surname: ''
    }
  }

  handleSubmit = (e) => {
    e.preventDefault();
    // send data from the actual state
  }

  handleChange = (e) => {
    this.setState({[e.target.name]: e.target.value})
  }

  render() {
    return (
      <form onSubmit={this.handleSubmit}>
        <input type="text" name="name" onChange={this.handleChange} />
        <input type="text" name="surname" onChange={this.handleChange} />
        <input type="submit" value="submit" />
      </form>
    )
  }
}

Take a look at this JSFiddle I did for you!看看我为你做的这个JSFiddle And check your console when sending the input.并在发送输入时检查您的控制台。

You might want to check out redux-form .您可能想查看redux-form Its great tool for form validation!它是表单验证的好工具!

Its a bit of a challenge to get started with, but really smooth once you get the hang of it.开始时有点挑战,但一旦你掌握了它,就真的很顺利。

Also the docs are great.文档也很棒。

This question showed up for in a google search just now so I thought I'd recommend formik https://github.com/jaredpalmer/formik这个问题刚刚出现在谷歌搜索中,所以我想我会推荐 formik https://github.com/jaredpalmer/formik

its very robust and performant + its nice if you dont want form state in your redux store它非常健壮和高性能 + 如果你不想在你的 redux 存储中使用表单状态,它很好

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

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