Winterfell icon indicating copy to clipboard operation
Winterfell copied to clipboard

questionAnswers state set to empty on rerender of <Winterfell />

Open geekyme opened this issue 8 years ago • 7 comments

I'm wrapping Winterfell with a component which accepts a prop role and use it to fetch schema data dynamically.

It seems like whenever Winterfell is rerendered, due to my wrapper component rerendering (due to prop change), it's questionAnswers state is being set to {};

Sample code:

/**
*
* RoleForm
*
*/

import React, { PropTypes } from 'react';
// import styled from 'styled-components';
import Winterfell from 'winterfell';
import { getRoleFormSchema } from './schemas';
// import { FormattedMessage } from 'react-intl';
// import messages from './messages';

class RoleForm extends React.PureComponent { // eslint-disable-line react/prefer-stateless-function
  constructor(props) {
    super(props);

    this.state = {
      schema: getRoleFormSchema(props.role),
    };
  }

  shouldComponentUpdate() {
    // TODO the moment Winterfell rerenders, it causes questionAnswers state to disappear
    return false;
  }

  onFormRender() {

  }

  onFormUpdate() {
    // TODO check for branch changes and clear data from supplied schema
    // set a schema default value as undefined to clear it from the json payload on submit
    console.log('update', arguments);
  }

  onFormSwitchPanel() {

  }

  onFormSubmit(questionAnswers, target) {
    console.log('submit', arguments);
  }

  render() {
    return (
      <Winterfell
        disableSubmit
        schema={getRoleFormSchema()}
        onRender={this.onFormRender}
        onUpdate={this.onFormUpdate}
        onSwitchPanel={this.onFormSwitchPanel}
        onSubmit={this.onFormSubmit}
      />
    );
  }
}

RoleForm.propTypes = {
  role: PropTypes.object,
};

export default RoleForm;

Debugger:

screen shot 2017-03-19 at 2 49 52 am

The problem will happen the moment I remove that shouldComponentUpdate block above.

geekyme avatar Mar 18 '17 18:03 geekyme

Hi @geekyme I believe this is by design, your parent component should be capturing the updates to answers and storing them, passing them back down. Otherwise there would be no "acceptable" way to flush out answers when you wanted to clear the form for example. Hope this helps!

andrewhathaway avatar Mar 20 '17 20:03 andrewhathaway

@andrewhathaway so how do i pass them back down?

geekyme avatar Mar 21 '17 19:03 geekyme

@geekyme You can pass them down using the questionAnswers prop. Let me know how you get on, and we can close this issue (I'll reopen it now).

andrewhathaway avatar Mar 21 '17 19:03 andrewhathaway

I tried that, it doesn't seem to work when the form rerenders with the new props. The prepopulated questionAnswers doesn't fill up the form.

geekyme avatar Mar 24 '17 18:03 geekyme

I'll investigate this at some point this weekend and have a look.

andrewhathaway avatar Apr 28 '17 18:04 andrewhathaway

I've taken a quick look and I cannot reproduce this. Can you get me a test case please? Use this as a base: https://esnextb.in/?gist=07a8b8d62af04bb39693f8bae5344a8b

andrewhathaway avatar Apr 29 '17 17:04 andrewhathaway

I got this problem as well. Just use shouldComponentUpdate() { // TODO the moment Winterfell rerenders, it causes questionAnswers state to disappear return false; } as above and then it is fine.

JonathanLehner avatar Mar 25 '18 12:03 JonathanLehner