Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setState changed value doesn't reflect on component #201

Closed
variable opened this issue Jul 21, 2019 · 4 comments
Closed

setState changed value doesn't reflect on component #201

variable opened this issue Jul 21, 2019 · 4 comments

Comments

@variable
Copy link

variable commented Jul 21, 2019

Hi I have the following console.log to see form component state changes:

_this.setValue = function (value) {
        var validate = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
        console.log(22222222222, validate, value);

        console.log(33333333333333333, _this.state);
        if (!validate) {
          _this.setState({
            value: value
          });
        } else {
          _this.setState({
            value: value,
            isValid: true,
            isPristine: false
          }, function () {
            // _this.context.formsy.validate(_this);
            console.log(5555555555555, _this.state);
          });
        }
      };

And the output is:

22222222222 true 15491
Wrapper.js:107 33333333333333332 {value: undefined, isRequired: true, isValid: false, isPristine: true, pristineValue: undefined, …}
Wrapper.js:120 5555555555555 {value: 15491, isRequired: true, isValid: true, isPristine: false, pristineValue: undefined, …}

Problem is that the component doesn't reflect the value change.

I also tried to print the state in render function within withformsy, but the state is different:
{isValid: false, isSubmitting: false, canChange: true}

It seems to me they are updating different component states?

@rkuykendall
Copy link
Member

Very interesting. I'm not as familiar with formsy state as some other devs on the project. Why are you calling state at all instead of props.setValue()?

@variable
Copy link
Author

I was using setValue() but it didn't work, so I went into the formsy code to see what setValue does

@rkuykendall
Copy link
Member

If setValue isn't working that seems like a more core issue, and should give us a clue. Which version of Formsy are you running? And is there anything interesting or unique about your codebase? Ideally, we could see a minimal example of the bug in a public repo. Maybe you could clone the project and remove most of the code until you have a minimal example?

@ThibautMarechal
Copy link
Contributor

I don't see any issue with what you have posted.
When calling setValue(15491)
The first console.log() will print arguments
The second will print the current state (before the update) the initial value is undefined
The third one print the state after the update (the value is 15491)

The next render of your component will get the latest value when calling getValue(); (It's just a function that give you "this.state.value" (https:/formsy/formsy-react/blob/master/src/Wrapper.ts#L212)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants