【问题标题】:React - setState with spread operator return a proxy objectReact - 带有扩展运算符的 setState 返回一个代理对象
【发布时间】:2019-01-16 09:49:31
【问题描述】:

这是我的代码,我不知道为什么,但 _onChangeValue 函数总是返回一个代理对象而不是一个简单的字符串值。我的 this.state.values 是一个有很多属性的长对象,这就是为什么我必须使用扩展运算符,该函数需要两个参数来设置状态,我尝试了很多次,甚至复制了创建者的代码但是一直失败,有什么解决办法吗?

这是函数

_onChangeValue = (name, value) => {
    this.setState(prevState => ({
      values: {
        ...prevState.values,
        [name]: value,
      },
    }));
  };

这些是文件

App.js

import React, { PureComponent } from 'react';

import Step from './Step';

export default class Wizard extends PureComponent {
  static Step = Step;

  state = {
    index: 0,

    values: {
      ...this.props.initialValues,
    },
  };

  _nextStep = () => {
    if (this.state.index !== this.props.children.length - 1) {
      this.setState(prevState => ({
        index: prevState.index + 1,
      }));
    }
  };

  _prevStep = () => {
    if (this.state.index !== 0) {
      this.setState(prevState => ({
        index: prevState.index - 1,
      }));
    }
  };

  _onChangeValue = (name, value) => {
    this.setState(prevState => ({
      values: {
        ...prevState.values,
        [name]: value,
      },
    }));
  };

  _onSubmit = () => {
    alert(JSON.stringify(this.state.values));
  };

  render() {
    console.log('values', this.state);
    return (
      <div style={{ flex: 1 }}>
        {React.Children.map(this.props.children, (el, index) => {
          if (index === this.state.index) {
            return React.cloneElement(el, {
              currentIndex: this.state.index,
              nextStep: this._nextStep,
              prevStep: this._prevStep,
              isLast: this.state.index === this.props.children.length - 1,
              onChangeValue: this._onChangeValue,
              values: this.state.values,
              onSubmit: this._onSubmit,
            });
          }

          return null;
        })}
      </div>
    );
  }
}

Wizard.js

 import React, { PureComponent } from 'react';

    import Step from './Step';

    export default class Wizard extends PureComponent {
      static Step = Step;

      state = {
        index: 0,

        values: {
          ...this.props.initialValues,
        },
      };

      _nextStep = () => {
        if (this.state.index !== this.props.children.length - 1) {
          this.setState(prevState => ({
            index: prevState.index + 1,
          }));
        }
      };

      _prevStep = () => {
        if (this.state.index !== 0) {
          this.setState(prevState => ({
            index: prevState.index - 1,
          }));
        }
      };

      _onChangeValue = (name, value) => {
        this.setState(prevState => ({
          values: {
            ...prevState.values,
            [name]: value,
          },
        }));
      };

      _onSubmit = () => {
        alert(JSON.stringify(this.state.values));
      };

      render() {
        console.log('values', this.state);
        return (
          <div style={{ flex: 1 }}>
            {React.Children.map(this.props.children, (el, index) => {
              if (index === this.state.index) {
                return React.cloneElement(el, {
                  currentIndex: this.state.index,
                  nextStep: this._nextStep,
                  prevStep: this._prevStep,
                  isLast: this.state.index === this.props.children.length - 1,
                  onChangeValue: this._onChangeValue,
                  values: this.state.values,
                  onSubmit: this._onSubmit,
                });
              }

              return null;
            })}
          </div>
        );
      }
    }

Step.js

import React, { PureComponent } from 'react';

class Step extends PureComponent {
  state = {};
  render() {
    return (
      <div>
        {this.props.children({
          onChangeValue: this.props.onChangeValue,
          values: this.props.values,
        })}
        <div>
          <button
            title="Prev"
            disabled={this.props.currentIndex === 0}
            onClick={this.props.prevStep}
          >Prev</button>
          {this.props.isLast ? (
            <button  onClick={this.props.onSubmit}></button>
          ) : (
            <button  onClick={this.props.nextStep}>Next</button>
          )}
        </div>
      </div>
    );
  }
}
export default Step;

Input.js

import React, { PureComponent } from 'react';

class Input extends PureComponent {
  _onChangeText = text => {
    this.props.onChangeValue(this.props.name, text);
  };

  render() {
    const { onChangeValue, name, ...rest } = this.props;
    return (
      <input {...rest} onChange={this._onChangeText} />
    );
  }
}

export default Input;

谢谢!

【问题讨论】:

  • 您能否附上显示您如何使用此_onChangeValue 函数的代码?
  • 好的!给我一秒钟

标签: javascript reactjs spread-syntax


【解决方案1】:
<input {...rest} onChange={this._onChangeText} />

事件对象作为第一个参数传递给_onChangeText,而不是输入字段中的文本。您可以从event.target.value获取文本。

<input {...rest} onChange={event => this._onChangeText(event.target.value)} />

【讨论】:

  • 天哪!这是一个愚蠢的错误!但是这段代码来自我看到的youtuber,它怎么不起作用?哈哈非常感谢!!
  • 别担心!玩得开心项目:)
猜你喜欢
  • 2020-06-22
  • 2019-08-10
  • 2021-04-09
  • 2020-10-04
  • 1970-01-01
  • 1970-01-01
  • 2018-10-04
  • 2020-11-15
  • 2021-01-16
相关资源
最近更新 更多