简体   繁体   English

用输入对插入数据做出反应

[英]react insert data with input

I just got into ReactJS. 我刚接触ReactJS。 I'm having a lot of problems learning it, everytime I get demotivated while working with it. 每当我在使用它时感到沮丧时,我在学习它时都会遇到很多问题。 I want to insert data into h1 with input. 我想用输入将数据插入到h1中。 I followed some tutorials, googled some code but I couldn't make it work. 我遵循了一些教程,在谷歌上搜索了一些代码,但无法使其正常工作。

 export default class Header extends React.Component{ constructor(){ super(); this.state={title:''}; this.handleChange = this.handleChange.bind(this); this.changeHtml = this.changeHtml.bind(this); } changeHtml(title){ this.setState({title:title}); } handleChange(event){ const textInput = event.target.value; this.props.changeHtml(textInput); } render(){ return ( <div> <h1 title={this.props.title} changeHtml={this.changeHtml} > </h1> <label> Name: <input type="text" name="name" onChange={this.handleChange} /> </label> </div> ); } } 

also could you tell me what is the diffrence between state and props ? 还可以告诉我,国家与道具之间的区别是什么? and what does they actually mean or refer to ( ex: input or something else) i dont know if i should keep up with react or learn another language such as angular 以及它们实际上是什么意思或指的是什么(例如:输入或其他),我不知道我是否应该跟上反应或学习其他语言,例如语言

I remove some boilerplate from your code, I guess it could help you as starting point, but you need to understand what is the real sense of "state" and "props". 我从代码中删除了一些样板,我想它可以作为起点,但是您需要了解“状态”和“道具”的真正含义。 https://jsfiddle.net/69z2wepo/68722/ https://jsfiddle.net/69z2wepo/68722/

class Header extends React.Component{

constructor(){
    super();
    this.state={title:''};
    this.handleChange = this.handleChange.bind(this);
}

handleChange(event){
    const textInput = event.target.value;
    this.setState({title:textInput});
}

render(){
        return (
            <div>
               <h1>{this.state.title}</h1>
                    <label>Name:
               <input type="text" onChange={this.handleChange} />
                    </label>
            </div>);
    }
}

ReactDOM.render(
  <Header />,
  document.getElementById('container')
);

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

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