React - setState with spread operator return 一个代理对象

React - setState with spread operator return a proxy object

这是我的代码,我不知道为什么 _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;

谢谢!

<input {...rest} onChange={this._onChangeText} />

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

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