1

Every time I click the button a post is being created and displayed

But the input data isn't showing up

import React, { Component } from 'react'

export default class App extends Component {
  state = {
    dataArr : [],
    isClicked: false,
    title : '',
    img : ''
  }

  handleSubmit = (e) => {
    e.preventDefault()
  }

  handleChange = (e) => {
    [e.target.name] = e.target.value
  }

  handleClick = () => {
    const copyDataArr = Object.assign([], this.state.dataArr)

    copyDataArr.push({
      title : this.state.title,
      img : this.state.img
    })

    this.setState({
      isClicked : true,
      dataArr : copyDataArr
    })

  }

  render() {
    console.log(this.state.title)
    return (
      <div>
          <form onSubmit={this.handleSubmit}>
          <input type="text" name="title"  placeholder="Title.." onChange={this.handleChange}/>
          <input type="text" name="img" placeholder="Image.." onChange={this.handleChange}/>
          <button onClick={this.handleClick}>SUBMIT</button>
          </form>
          { this.state.isClicked ? 
              this.state.dataArr.map(
                (post, index) => { 
                  return(
                    <div class="div">
                      <h1>title: {post.title}</h1> 
                      <img src={post.img} alt="ohno"/>
                    </div> 
                  )

                }
              ) 
              : null }
      </div>
    )
  }
}

Created an empty array on state and copied the array and pushed in the input values. What am I doing wrong?

Need the onChange to work and the input data to display

Trying to learn forms in react. Hope someone can help out

2 Answers 2

1

Your handleChange function is not updating state with the data you enter to the inputs. Looks like you might have forgotten to call this.setState(). Try this :

  handleChange = (e) => {
    this.setState({
      [e.target.name]: e.target.value
    })
  }

Also here is a sandbox with the working code: https://codesandbox.io/s/distracted-darkness-xp3nu

Sign up to request clarification or add additional context in comments.

5 Comments

That doesn't appear to be the case in the code you posted above @Parsa. As I've suggested, I'm able to get your posts to display correctly by just adjusting the handleChange()
oh i see the problem, it was very stupid, i didnt set state, thanks man
No worries! @Parsa You literally had everything else working though which is a good sign. :)
did you change anything else though? doing that still didnt work on my code although your sandbox code is working just fine
I updated the handleChange. That's all that was needed to get your data to show up. Additionally I made it so the value prop of each input matches a field in the state, which just makes it easier to clear the forms, which is not necessary. Other than that no.
0

You have to add a value attribute to the input tag and pass the state value.

<input type="text" name="title" value={this.state.title}  placeholder="Title.." onChange={this.handleChange}/>

Your handle change function should be as follows

  handleChange = (e) => {
    this.setState({[e.target.name] : e.target.value});
  }

2 Comments

when i did that, i was unable to even type on the input anymore? it wouldnt let me type on the input after doing that
Impressive deduction.

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.