0

Hello I am trying to render my PostOnWall component I made using an onClick function. The goal that every time someone clicks the button handleClick will render one new component on the screen each time. So if I click the button three times i should see three PostOnWall components rendered on my screen. Please tell me what I am doing wrong.

class Textbox extends Component {
    constructor(props) {
      super(props);
      this.handleClick.bind(this);
      this.state = {
        textArea: "",
        text: "",
        show: false,
        curTime : new Date().toLocaleString(),

      };
    }

    handleChange(event) {
      const myValue = event.target.value;
      this.setState({
        textArea: myValue
      })
      console.log(this.state)
    }

    handleClick= () => {
      this.setState({text:this.state.textArea,
      show: !this.state.show});
      return (
      <div>
          {this.state.show &&   <PostOnWall PostOnWall={this.props.PostOnWall} text={this.state.text} time={this.state.curTime}/>}
        </div>
        );
    }


    showNewPost

      render() {
        return (
        <div>
            <textarea className="Textbox" 
            rows="2" cols="30"
             type = "text" 
             onChange={this.handleChange.bind(this)}
             value={this.state.textArea} >
            </textarea>
              <button className="postbutton" onClick={this.handleClick.bind(this)}>Post</button>
        </div>

        );
    }
  }



export default Textbox;
1
  • Returning the component from the onClick handler will do nothing. You need to add the PostOnWall component in the render method. You could create an array in local state and every time you click the button you add a component to that state, then in the render method render that array of components Commented Apr 5, 2020 at 14:33

2 Answers 2

1

That should do the trick for you;

import React, { Component } from 'react';

class Textbox extends Component {
  constructor(props) {
    super(props);
    this.handleClick.bind(this);
    this.state = {
      textArea: '',
      text: '',
      show: false,
      curTime: new Date().toLocaleString(),

    };
  }

  handleChange = (event) => {
    const myValue = event.target.value;
    this.setState({
      textArea: myValue
    });
  }

  handleClick= () => {
    this.setState({
      text: this.state.textArea,
      show: !this.state.show
    });
  }

  render() {
    return (
      <div>
        <textarea
          className="Textbox"
          rows="2"
          cols="30"
          type="text"
          onChange={this.handleChange.bind(this)}
          value={this.state.textArea}
        />
        <button
          className="postbutton"
          onClick={this.handleClick}
          type="button"
        >
          Post
        </button>
        {
          this.state.show &&
            <PostOnWall
              PostOnWall={this.props.PostOnWall}
              text={this.state.text}
              time={this.state.curTime}
            />
        }
      </div>

    );
  }
}

You should use the function called on click to change the state only. Then render (or not) the PostOnWall component based on the state value.

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

2 Comments

Ya that was the first solution I came up with but all it does is hide and show the component. I want render a new PostOnWall componenet every time I click the button
Then just add an array of posts in the state and use a map in the render just like: { posts.map((post) => (<PostOnWall PostOnWall={this.props.PostOnWall} text={this.state.text} time={this.state.curTime} />));
0

you need to add state which increase on click and then render the component depending on how many time the button is clicked. here the codeSandbox for what you are trying to achieve.

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.