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

Feature Proposal: formValues() HOC decorator that works inside a form component #2996

Closed
erikras opened this issue May 31, 2017 · 14 comments
Closed
Labels
enhancement improvements in current API

Comments

@erikras
Copy link
Member

erikras commented May 31, 2017

From discussion on #2852 with @wmertens. The problem being solved is that formValueSelector() cannot be used in a reusable way inside a <FormSection> because it doesn't know what the prefix defined in the <FormSection> is. General API syntax would look like:

@formValues({
  destProp: 'nameOfField'
})
class MyReusableFields extends Component {
  render() {
    const { destProp } = this.props
    // destProp is the value of the field called 'nameOfField'
    return (
      <div>
        <Field name="nameOfField" component="input"/>
        ... other fields

        ... some logic involving value of nameOfField:
        {destProp === 'specialValue' && <div>Value is SPECIAL!!</div>}
      </div>
    )
  }
}

There might also be an alternative syntax where, if your keys and values were all going to be the same, rather than pass...

@formValues({
  firstField: 'firstField',    // key and value are ===
  secondField: 'secondField'   // key and value are ===
})
class MyReusableFields extends Component {
  ...
}

...you could just pass a list of strings. e.g.

@formValues('firstField', 'secondField')
class MyReusableFields extends Component {
  ...
}
@wmertens
Copy link
Contributor

wmertens commented Jun 4, 2017

Alright, I took a stab at implementing it. It's using @connect to listen to store updates, but it uses the getValues() from context._reduxForm to get at the values. See #3011

@erikras
Copy link
Member Author

erikras commented Jun 8, 2017

Merged #3011. Will comment again when released.

@erikras erikras closed this as completed Jun 8, 2017
@erikras
Copy link
Member Author

erikras commented Jun 9, 2017

Fix published in v6.8.0.

@jasan-s
Copy link

jasan-s commented Jun 15, 2017

I am sure I'm using the feature incorrectly. Instead of reading from Redux directly I thought I could use Values = formValues('email','password')(Auth) the Auth is the form name and the Field names are email and password.

@jasan-s
Copy link

jasan-s commented Jun 15, 2017

I wanted to access forms values within the component. I know I can pass it down by using connect but though this may be cleaner.

@wmertens
Copy link
Contributor

wmertens commented Jun 15, 2017 via email

@jasan-s
Copy link

jasan-s commented Jun 15, 2017

@wmertens so, what is the recommended way to accesses the form field values within the same form component? should i add something to the main export

export default reduxForm({
  form: 'Auth',
  destroyOnUnmount: false,
  validate,
})(Auth)

@wmertens
Copy link
Contributor

wmertens commented Jun 15, 2017 via email

@wmertens
Copy link
Contributor

wmertens commented Jun 15, 2017 via email

@gleb-lobastov
Copy link

@erikras I face some strange behaviour with formValues: values lagging behind the state at the time of reading. getValues retun { flag: false } when it actually true in the state. They syncs only on next mapStateToProps calculation, which might be performed only at next user interaction.

So formValuesSelector works for me, but formValues does not =(

@wmertens
Copy link
Contributor

@gleb-lobastov can you share the code where you are using it? The @formValues just creates a redux listener with connect, so it should be updating when the store is updating, like all other connected components. Try looking at the redux and react devtools to see what the redux state is and what the react components get for props?

@gleb-lobastov
Copy link

@wmertens finally I reproduce the issue:
https://codesandbox.io/s/Vrn7MZov

And root of problem was in my react-redux version: 4.4.5
if you change react-redux to latest 5^ then problem will gone

@wmertens
Copy link
Contributor

@gleb-lobastov wow, that's surprising! Good that you were able to fix it!

@lock
Copy link

lock bot commented Jul 23, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Jul 23, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement improvements in current API
Projects
None yet
Development

No branches or pull requests

4 participants