This is my code, i don't know why but the _onChangeValue function is always returning a proxy object instead of a simple string value. My this.state.values is a long object with a lot of properties, that's why i have to use the spread operator, the function takes two parameters to set the state, i tried a lot of times and even copied the code of the creator but keeps failing, is there any solution?
this is the function
_onChangeValue = (name, value) => {
this.setState(prevState => ({
values: {
...prevState.values,
[name]: value,
},
}));
};
and these are the files
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;
Thanks !