0

Trying to figure out how to dynamically handle a varying amount of props data for a form. My google hunt has come up with nothing. I'm collecting data on number of appointments based on number dogs a related user has. So props would look like this:

this.props: {
  user: {
    id: 11
    name: "Steve",
    age: 32
  }
  dogs: [{
    id: 18,
    name: "Rover"
  },{
    id: 42,
    name: "Snugglemittens"
  }]
}

Every user has a different number of dogs. And my parent element view looks like this:

render(){
    <form onSubmit={this._handleSubmit.bind(this)}>
      {this.props.dogs.map((dogge)=>{
        return(<MsgView dog={dogge} key={dogge.id} />;)
      }}
      <button type="submit">Submit</button>
    </form>
}

Then inside each Message view I want to collect two check boxes about each dog:

render(){
  <div className="form-group" data-toggle="buttons">
    <label><input type="checkbox" autoComplete="off"/> XYZ </label>
  </div>
  <div className="form-group" data-toggle="buttons">
    <label><input type="checkbox" autoComplete="off"/> ABC </label>
  </div>
}

I'm not sure how to get the filled in data back to my backend using ajax. My initial thought was to use state to collect the info and send it back all at once. I'm pretty sure React can pass a function through a prop that I can use, but I can't find a good example.

8
  • Have you investigated Redux-form? Commented Oct 2, 2017 at 18:40
  • 1
    This would have you creating a handleFormSubmit() method that initiates or sends a request with the data to your backend. Commented Oct 2, 2017 at 18:41
  • @agm1984 Can you direct me to an example or documentation to help me out? Commented Oct 2, 2017 at 20:29
  • redux-form.com/7.0.4/examples/simple Commented Oct 2, 2017 at 20:50
  • This is the industry standard way. Stephen Grider has some awesome tutorials on Udemy also, if you want to explore it effectively as fast as possible. Commented Oct 2, 2017 at 20:52

1 Answer 1

1

This is what I'm thinking could work. You could populate this.state when the view loads,

// or in a lifecycle method componentWillMount or componentDidMount instead
// and do in constructor, this.state = { dogs: [] }
// sorry, this is kind of hard to do abstractly

constructor(props) {
  super(props)
  // on pageload, set initial state to the doggo array
  this.state = {
    dogs: this.props.dogs
  }
}

// alternate solution potential:
componentDidMount() {
  this.setState({ dogs: this.props.dogs })
}

_handleSubmit() {
  console.log('FIELDS:', this.state.dogs)
  // now we're in business if you see your data
}

render(){
    if (this.state.dogs.length === 0) return <div>No dogs yet, loading spinner</div>
    return (
      <form onSubmit={() => this._handleSubmit()}>
        {this.state.dogs.map((dogge)=> {
          return(
            <MsgView
              dog={dogge}
              key={dogge.id}
              {/* on change, update state of field */}
              onChange={(e) => this.setState({
                {/* ie: dogs[0], dogs[45]
                    you might have to use ES6 .find() or something
                    you guys/girls see where I'm going with this
                    basically update the array or make better data structure.
                    I like the idea of this.state.dogs[doggo]
                    (so dogs is an object with dynamic keys)
                    then you can just update the value of that key with setState */}
                dogs[this.state.dogs.indexOf(dogge)]: e.target.value
              })}
            />
          )}
        }
        <button type="submit">Submit</button>
      </form>
    )
  }

I don't remember if its e.target.value, but you should see what I'm getting at.

  1. You transfer dogs from props to component state
  2. keep state up to date
  3. read from it after submit
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.