简体   繁体   English

使用React,JSX:如何检索输入值,将其作为元素发布,然后清除以重复

[英]Using React, JSX: how to retrieve input value, post it as an element, and then clear it to repeat

This is an assignment for school. 这是学校的作业。 It involves using React and JSX to create an input field and a submit button. 它涉及使用React和JSX创建输入字段和提交按钮。 When the button's clicked, the input value should render as an element to the body. 单击按钮时,输入值应呈现为主体的元素。 I was able to create it for the first click, but don't know how to repeat it. 我能够在第一次点击时创建它,但不知道如何重复。

If you look at the code below, you'll see that when user types, handleChange changes state of input and when the button's clicked, handleClick changes the boolean state of the button (called 'post'). 如果查看下面的代码,您将看到,当用户键入内容时,handleChange会更改输入状态,而在单击按钮时,handleClick会更改按钮的布尔状态(称为“发布”)。 If post is true, the input along with a timestamp is rendered as a heading. 如果post为true,则将输入与时间戳一起呈现为标题。

The problem is that after the render, the input isn't cleared. 问题在于渲染之后,输入不会被清除。 If the user changes input and clicks button again, it updates the heading with a new timestamp and new input instead of adding another heading. 如果用户更改输入并再次单击按钮,它将使用新的时间戳和新的输入来更新标题,而不是添加其他标题。

I've tried changing back state for input and post in handleClick, handleChange, componentDidMount, and componentDidUpdate. 我已经尝试过更改输入和发布在handleClick,handleChange,componentDidMount和componentDidUpdate中的状态。 But that repeatedly calls setState and I get an error message 'maximum update depth exceeded.' 但这反复调用setState,我收到一条错误消息“超出最大更新深度”。

So, what I want it to do is post a new heading of the input value every time the user clicks the post button. 因此,我要执行的操作是每当用户单击发布按钮时就发布输入值的新标题。 I also want it to clear the input/placeholder text. 我也希望它清除输入/占位符文本。

 import React, { Component } from 'react'; import './App.css'; import Firstposts from './firstposts.jsx'; class App extends Component { constructor(props) { super(props) this.state = { input: "", post: false } this.handleChange = this.handleChange.bind(this); this.handleClick = this.handleClick.bind(this); } handleChange(event) { this.setState({ input: event.target.value }); } handleClick() { this.setState({ post: true }) } render() { let timestamp = new Date(); return ( <div className="container"> <div className="panel"> <img height="100 px" src="https://marketing.twitter.com/content/dam/marketing-twitter/brand/logo.png" alt=""></img> <h1>Chirper</h1> </div> <div className="body"> <input placeholder="type your message here" onChange={this.handleChange} /> <button onClick={this.handleClick} >Post</button> <h2>Log</h2> {<Firstposts />} {this.state.post ? <div> <h3>{timestamp.toString()}</h3> <h4>{this.state.input}</h4> </div> : <div /> } </div> </div > ); } } export default App; 

Update your handleClick method to set posts to an array of posts, instead of a boolean: 更新您的handleClick方法以将posts设置为posts数组,而不是布尔值:

handleClick() {
  this.setState({
    posts: [
      ...this.state.posts,
      this.state.input
    ]
  })
}

This will add the value of this.state.input to the end of this.state.posts , preserving all previous posts. 这将价值添加this.state.input到年底this.state.posts ,保留所有的以前的职位。

You can update this further to clear the value of the input field: 您可以进一步更新它以清除输入字段的值:

handleClick() {
  this.setState({
    posts: [
      ...this.state.posts,
      this.state.input
    ],
    input: '' // add this line to clear your input field when a new post is submitted
  })
}

Also, make sure to give your <input> element a value of this.state.input : 另外,请确保为您的<input>元素this.state.inputvalue

<input
  value={this.state.input}
  placeholder="type your message here"
  onChange={this.handleChange}
/>

Without this, you will not be able to programmatically update the value of the <input> field by using setState . 否则,您将无法使用setState以编程方式更新<input>字段的值。 You can read more on uncontrolled components in React . 您可以在React中阅读更多有关不受控制的组件的信息

Then, update your render method to map over this.state.posts and render each one: 然后,更新您的render方法以mapthis.state.posts并渲染每个:

{this.state.posts.map(post => (
  <h4>{post}</h4>
))}

You can read more on rendering lists in React . 您可以在React中阅读有关渲染列表的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM