0

Implemented writing data from the form to the array. Everything works. but now I want to implement data output when I click on " SEND ARR " When I hang up a click on a button, respectively, the data is not displayed since in the function we access event.target.value

Please tell me how to rewrite the line so that I can display data when I click on the button? thank

home.js

import React from "react";
import "./../App.css"


export default class Home extends React.Component {
    constructor() {
        super()


        this.state = {
            count: 1,
            objexm: '',
            inputValue: '',
            arr: []
        }
        this.handleClick = this.handleClick.bind(this);
        this.updateInputValue = this.updateInputValue.bind(this);
    }

    handleClick() {
        this.setState({
            count: this.state.count + 1
        });
    };


    createMarkup() {
        return {
            __html: this.state.inputValue
        };
    };

    updateInputValue(event) {
        let newArr = this.state.arr;
        let newlist = event.target.value;
        if (event.target.value) {
            newArr.push(newlist)
        }
        this.setState({
            inputValue: newArr
        });
        event.target.value = '';
    };


    render() {
        return (

            <div className="home-header">
                <h2>{this.state.count}</h2>
                <button onClick={this.handleClick}>Add</button>

                <input type='text' name="names" onClick={this.updateInputValue} />

                {this.state.arr.map((arrs, index) => {
                    return (
                        <li
                            key={index}
                        >{arrs}</li>
                    )
                })}

<button>SEND ARR</button>
                <ul className="qwe">
                    <li dangerouslySetInnerHTML={this.createMarkup()}></li>
                </ul>
            </div>


        );
    }
}
1
  • where do you want to output your data? Commented Apr 27, 2020 at 9:29

2 Answers 2

1

Store the data from the input into invputValue each time the input is updated and on the click of the button update the arr content with the old values (...arr) plus the current input value (this.state.inputValue) . To make sure the old values are not deleted the arr is defined at the top of the class let arr = []. If you don't want it there you can instantiate it in the constructer which will run only once. i.e. this.arr = []

let arr = []
class Home extends React.Component {
    constructor() {
        super()
        this.state = {
            count: 1,
            objexm: '',
            inputValue: '',
            arr: []
        }
        this.handleClick = this.handleClick.bind(this);
        this.updateInputValue = this.updateInputValue.bind(this);
    }

    handleClick() {
        this.setState({
            count: this.state.count + 1
        });
    };

    createMarkup() {
        return {
            __html: this.state.inputValue
        };
    };

    updateInputValue = e => {
      this.setState({ inputValue: e.target.value })
    }

    displayData = () => {
      arr = [...arr ,this.state.inputValue]
      this.setState({ arr, inputValue: "" })
    }

    clearData = () => {
      this.setState({ arr: [] })
    }

    render() {
      console.log("this.state.arr:", this.state.arr)
        return (

            <div className="home-header">
                <h2>{this.state.count}</h2>
                <button onClick={this.handleClick}>Add</button>

                <input type='text' name="names" onChange={this.updateInputValue} value={this.state.inputValue} />

                {this.state.arr.map((arrs, index) => {
                    return (
                        <li
                            key={index}
                        >{arrs}</li>
                    )
                })}

                <button onClick={this.displayData}>SEND ARR</button>
                <button onClick={this.clearData}>CLEAR ARR</button>
                <ul className="qwe">
                    <li dangerouslySetInnerHTML={this.createMarkup()}></li>
                </ul>
            </div>
        );
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

thank! And yet, how to clear the form after clicking on the button?
Do you mean the list? If you want to clear just make another button and in that button reset arr [this.setState({arr: []})]
meant to clear the "input field" itself after sending,.,after a click, the values ​​in the input field remain and are not erased
I update the code with the changes you want. If you're satisfied you can vote me up.
Thank you very much for taking the time to respond. I understand correctly that: this.setState ({arr, inputValue: ""}) replaces "arr" with "arr from state" and adds an empty "inputValue"?
|
1

Instead of using onClick on input, use onChange and update value in state i.e. make the input a controlled component . Post that onClick of button take the value from state and push to the array and clear the input value

export default class Home extends React.Component {
    constructor() {
        super()


        this.state = {
            count: 1,
            objexm: '',
            inputValue: '',
            arr: []
        }
        this.handleClick = this.handleClick.bind(this);
        this.updateInputValue = this.updateInputValue.bind(this);
    }

    handleClick() {
        this.setState(prevState => ({
            count: prevState.count + 1,
            inputValue: [...prevState.inputValue, prevState.name],
            name: ""
        }));
    };


    createMarkup() {
        return {
            __html: this.state.inputValue
        };
    };

    updateInputValue(event) {
        let newVal = event.target.value;
        this.setState({
            name: newVal
        });
    };


    render() {
        return (

            <div className="home-header">
                <h2>{this.state.count}</h2>
                <button onClick={this.handleClick}>Add</button>

                <input type='text' name="names" value={this.state.name} onChange={this.updateInputValue} />

                {this.state.arr.map((arrs, index) => {
                    return (
                        <li
                            key={index}
                        >{arrs}</li>
                    )
                })}

<button>SEND ARR</button>
                <ul className="qwe">
                    <li dangerouslySetInnerHTML={this.createMarkup()}></li>
                </ul>
            </div>


        );
    }
}

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.