Git Product home page Git Product logo

Comments (2)

danielepiccone avatar danielepiccone commented on May 14, 2024

It turns out the problem is solved by passing { pure: false } as an option to connect() since redux assumes a component to receive updates either from props or from the store.

Alternatively you could use something like this, by connecting the component that receives the formAPI inside the FormField

import React from 'react';
import { FormField } from 'react-form';
import { connect } from 'react-redux';

function FormInput({ foobar, getValue, setValue }) {
  const value = getValue() || 0;
  const onClick = () => {
    setValue(value + 1);
  }

  return (
    <div>
      <p>click me</p>
      <p>{foobar}</p>
      <button onClick={onClick}>Clicked { value } times</button>
    </div>
  )
}

const mapStateToProps = state => {
  return {
    foobar: state.value
  }
};

FormInput = connect(mapStateToProps)(FormInput)

function Input(props) {
  return (
    <FormField field='foo'>
      {props => <FormInput {...props} />}
    </FormField>
  )
};

export default Input;

Or alternatively:

import React from 'react';
import { FormField } from 'react-form';
import { connect } from 'react-redux';
import { compose } from 'redux';

function withFormField(field) {
  return WrappedComponent => props => {
    return (
      <FormField field={field}>
        {(formApi) => {
          const allProps = {
            ...props,
            ...formApi
          };
          return (
            <WrappedComponent {...allProps} />
          )
        }}
      </FormField>
    )
  }
}

function Input({ foobar, getValue, setValue }) {
  const value = getValue() || 0;
  const onClick = () => {
    setValue(value + 1);
  }

  return (
    <div>
      <p>click me</p>
      <p>{foobar}</p>
      <button onClick={onClick}>Clicked { value } times</button>
    </div>
  )
}

const mapStateToProps = state => {
  return {
    foobar: state.value
  }
};

export default compose(
  withFormField('foo'),
  connect(mapStateToProps)
)(Input);

Is there any reason FormField is not provided as an higher order component but instead one should pass a function as the first child?

from form.

tannerlinsley avatar tannerlinsley commented on May 14, 2024

It was previously. Technically you could easily create your own HOC that utilizes the current form component implementation under the hood. :)

from form.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.