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

Slow render speed for complex forms #19

Closed
vladra opened this issue Dec 28, 2016 · 4 comments
Closed

Slow render speed for complex forms #19

vladra opened this issue Dec 28, 2016 · 4 comments

Comments

@vladra
Copy link

vladra commented Dec 28, 2016

I've encountered issue with pretty complex form being slow to render (because all form parts are re-rendered).

I've solved it with custom defined Inputs, where form value is stored in custom input state. Using this state I can compare it with nextState in shouldComponentUpdate.

class CustomInput extends Component {
  constructor (props) {
    super(props)
    this.handleChange = this.handleChange.bind(this)
  }

  state = { value: '' }

  shouldComponentUpdate (nextProps, nextState) {
    return this.state.value !== nextState.value
  }

  handleChange (setValue) {
    return (e) => {
      const { value } = e.target
      this.setState({ value })
      setValue(value)
    }
  }

  render () {
    return (
      <FormField field={this.props.field}>
        {
          ({ setValue, getValue, setTouched }) =>
            <input
              value={getValue()}
              onChange={this.handleChange(setValue)}
              onBlur={setTouched}
            />
        }
      </FormField>
    )
  }
}

So as you see, I need to keep internal custom form input state in sync with react-form state. And thats not the best solution in my opinion. I'm doing this because I didn't find any other way to get input value inside shouldComponentUpdate (manually passing getValue function to each input is not ideal too).

Do you have any suggestions on how to improve this case?

Could be related to #4 and #5

@vladra vladra changed the title Render speed Slow render speed for complex forms Dec 28, 2016
@vladra
Copy link
Author

vladra commented Dec 28, 2016

And actually this approach doesn't work if I need to change form state outside of specific input field (for example load data to form on button click)

@tannerlinsley
Copy link
Collaborator

How does your form grow? Do you simply have a lot of inputs? Or do you have extremely deep objects as your values?

@vladra
Copy link
Author

vladra commented Dec 29, 2016

I have form which is built based on values it has, with ability to add new values. For example tariffs[0].name, tariffs[0].price. About 30-40 fields on average. 10-15 of them are multiselects, few text inputs and others are selects.

I've found the you provide access to formAPI via context, so this solved my problem for now. I still need to store input value somewhere (outside of form state), to be able to compare current formValue (accessible via context formAPI) with previous (stored inside of input component).

Don't think it's possible to speed it up without shouldComponentUpdate. Also if there was an easy way to access previous value (without storing it manually), that could be useful.

Anyway, if you have better approach, let me know :)

@tannerlinsley
Copy link
Collaborator

Yeah, optimizing for extremely large forms here would potentially touch all points of the library, so I think we need to ponder how we would accomplish this a bit more before we go diving in :)

Closing this issue for now since you were able to skirt around things with access to the internal state, but let's keep the discussion going as we find better ways to optimize things.

This issue was closed.
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

2 participants