简体   繁体   English

setState和JSX道具不应使用箭头功能

[英]setState and JSX props should not use arrow functions

I have read similar questions here 我在这里读过类似的问题

Why shouldn't JSX props use arrow functions or bind? 为什么JSX道具不应该使用箭头功能或绑定?

JSX props should not use .bind() - how to avoid using bind? JSX道具不应该使用.bind()-如何避免使用bind?

No .bind() or Arrow Functions in JSX Props making no sense to me when I need to pass arguments 当我需要传递参数时,JSX Props中没有.bind()或Arrow函数对我来说毫无意义

And I understand how using an arrow function causes the function to be recreated on each render and effects performance. 而且我了解使用箭头功能如何导致在每次渲染时重新创建该功能并影响效果。 However I still don't fully understand how to resolve this issue in React Native, specifically when using setState. 但是我仍然不完全了解如何在React Native中解决此问题,特别是在使用setState时。

For example, if I have a TextInput that updates a value held in the component state in the onChangeText function, how do I avoid using an arrow function? 例如,如果我有一个TextInput更新了onChangeText函数中组件状态中保存的值,那么如何避免使用箭头函数?

<TextInput
  value={this.state.text}
  onChangeText={text => this.setState({ text })}
  />

Must I create a handler for each property in the state that I want to update? 我是否必须为要更新状态的每个属性创建一个处理程序? For example, if I have two TextInput fields Email and Password, would that need to be handled like this? 例如,如果我有两个TextInput字段Email和Password,是否需要像这样处理?

  updateEmail = email => {
    this.setState({ email })
  }

  updatePassword = password => {
    this.setState({ password })
  }

  render() {
  ...
  <TextInput
      value={this.state.email}
      onChangeText={this.updateEmail}
      />
  <TextInput
      value={this.state.password}
      onChangeText={this.updatePassword}
      />

You can set id or name of an element same with the name of a state variable. 您可以将id或元素的名称设置为与状态变量的名称相同。 So you can create one function and pass it to all inputs like: 因此,您可以创建一个函数并将其传递给所有输入,例如:

onFieldChange = (e) => {
    const key = e.target.id
    const value = e.target.value
    this.setState({[key]:value})
}

Generic way 通用方式

Create your own TextInput component that takes an id prop and passes it as a second parameter when calling the handler. 创建您自己的TextInput组件,该组件需要一个id属性,并在调用处理程序时将其作为第二个参数传递。 This is the generic way to handle it: 这是处理它的通用方法:

class MyTextInput extends Component {

    handleTextChange = text => {
        const {id, onTextChange} = this.props;
        return onTextChange && onTextChange(text, id);
    }

    render() {
        return (
            <TextInput {...this.props} onTextChange={this.handleTextChange}
        );
    }
}

And use it like that: 并像这样使用它:

class App extends Component {

    handleTextChange = (text, id) => this.setState({[id]: text});

    render() {
        const {email, password} = this.state;

        return (
            <div>
                <MyTextInput
                    id="email"
                    value={email} 
                    onTextChange={this.handleTextChange}
                />
                <MyTextInput
                    id="password" 
                    value={password}
                    onTextChange={this.handleTextChange}
            </div>
        );
    }
}

As it does not use arrow functions it avoids performance penalties if you have a large amount of components. 由于它不使用箭头功能,因此如果您有大量组件,可以避免性能下降。 But note that the penalty is marginal for only a few components and not worth the effort in most of the cases. 但是请注意,处罚仅对少数几个组件来说是微不足道的,在大多数情况下不值得付出努力。 Just make sure you do not forward the handler that is an arrow function multiple levels deep. 只要确保您不转发深层箭头功能的处理程序即可。

Easy way for react-native TextInput 反应本机TextInput简单方法

Use the onChange handler which gets the event instead of only the text. 使用可以获取eventonChange处理程序,而不只是文本。 The event also contains the name of the input if one way provied to it: 如果提供了一种方法,该事件还包含输入的name

class App extends Component {

    handleChange = event => {
        const {name, value} = event.nativeEvent;
        this.setState({[name]: value});
    }

    render() {
        const {email, password} = this.state;

        return (
            <div>
                <MyTextInput
                    name="email"
                    value={email} 
                    onChange={this.handleChange}
                />
                <MyTextInput
                    name="password" 
                    value={password}
                    onChange={this.handleChange}
            </div>
        );
    }
}

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

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