0

I have a React project and it's using Recompose. Let's say I have a Form, and I supply a 'withHandler' to be used for ..

How can I also change the state of the React component when the Form is submitted?

John Doe
  • 113
  • 2
  • 7

1 Answers1

0

So let's say the form is submitted with a button, and we have a onClick attribute on the button.

It's a very simple example but hopefully shows you how you would update state with the onClick. Remember, this an attribute that can be applied on HTML elements. You can read about it the onClick attribute here.

import React, { Component } from 'react';

import React from "react";
import { render } from "react-dom";
import Component from "react-component-component";

class Button extends Component {
  state = {
    counter: 0
  };

  handleButtonClick = () => {
    this.setState({
      counter: this.state.counter + 1
    });
  };

  getButton = () => {
    const { text } = this.props;

    return (
      <button
        onClick={this.handleButtonClick}
      >
        {text}
        {this.state.counter}
      </button>
    );
  };

  render() {
    return <div>{this.getButton()}</div>;
  }
}

render(
  <Button text="press me to increase counter: " />,
  document.getElementById("root")
);

The following can be seen here: https://codesandbox.io/s/ly11qv0vr7

There is also a very good example of react documentation regarding handling events. You can read about handling events in react here. I believe the above link will provide you all the information needed to be able to handle a form being submitted.

Corbuk
  • 670
  • 1
  • 8
  • 23