简体   繁体   English

ReactJS:如何正确使用此textInput组件?

[英]ReactJS: How I do I use this this textInput component correctly?

Hi I'm super new to ReactJs. 嗨,我是ReactJs的超级新手。 I'm finding the docs to be difficult to digest, as such I'm having trouble with something very basic. 我发现文档难以理解,因此我遇到了一些非常基本的问题。 I'm trying to use this textinput component in my form and I don't know how to set value dynamically. 我正在尝试在表单中使用此textinput组件,但我不知道如何动态设置值。

This is the TextInput component 这是TextInput组件

import React, { PropTypes } from 'react';

import FormField from './FormField';

var TextInput = React.createClass({
  propTypes: {
    field: PropTypes.object.isRequired
  },
  render() {
    var {field, help, label, ...inputProps} = this.props
    return <FormField field={field} help={help} inputProps={inputProps} label={label}>
      <input
        {...inputProps}
        className="form-control"
        name={field.name}
        value={field.value}
        onBlur={field.onBlur}
        onChange={field.onChange}
      />
    </FormField>
  }
})

export default TextInput

This is where I'm using it 这是我使用的地方

import React from 'react';
import ProfileSideBar from './ProfileSideBar';
import ProfileSectionLabel from './ProfileSectionLabel';
import TextInput from '../forms/TextInput';

class ProfileHome extends React.Component {

render() {

  return <div id='profile-wrapper'>
   <tr width='100%'>
    <td width="33%"> Location </td>
       <td width="33%"> 
         <TextInput field={location} 
          style={{height: 40,
                  borderColor: 'orange', 
                  borderWidth: 1}}>
         </TextInput>
       </td>
   </tr>
  </div>

Inside where I use TextInput, I'm trying to set a default value. 在使用TextInput的地方,我尝试设置默认值。 So something like this: 所以像这样:

location{ value:'ny' } So if it exists, it'll populate ny, and if not it'll be blank. location {value:'ny'}因此,如果存在,它将填充ny,否则,将为空白。

I tried 我试过了

<TextInput value={value}> 

It just doesn't run. 它只是没有运行。 When I remove value = value the page renders but without the data I need of course. 当我删除value = value时,页面会呈现出来,但是我当然不需要数据。

I know I have to (or at least i think i know) Set the state or something and bind it to my profileHome component...I just don't know how. 我知道我必须(或者至少我想我知道)设置状态或某些东西并将其绑定到我的profileHome组件...我只是不知道如何。 If anyone can show me how to do this I would be so happy. 如果有人可以告诉我该怎么做,我会很高兴。 And if possible, throw in a good resource for me to look at. 如果可能的话,请提供大量资源供我参考。 I feel like angular was so much easier to pick up. 我觉得棱角分明要容易得多。

Have you tried using defaultValue ? 您是否尝试使用defaultValue

<TextInput defaultValue={value}> 

This will render whatever is passed as a defaultValue, however you will still need to bind the value and onChange to reflect user interaction since you are using <input> as a controlled component. 这将呈现作为defaultValue传递的任何内容,但是由于将<input>用作受控组件,因此您仍然需要绑定valueonChange以反映用户交互。 See React's Controlled Components for more information. 有关更多信息,请参见React的受控组件

Here's one way to go about it. 这是解决问题的一种方法。

First, create the TextInput class and set your initial state of the text value in the constructor function and bind the correct 'this' context to the method you're going to create next: 首先,创建TextInput类,并在构造函数中设置文本值的初始状态,然后将正确的“ this”上下文绑定到接下来要创建的方法:

class TextInput extends Component {
  constructor(props) {
    super(props);
    this.state = { text: 'ny' };
    this.onInputChange = this.onInputChange.bind(this);
  }

Then, create a method of the TextInput class that handles changes to the value of the input and sets the state accordingly: 然后,创建TextInput类的方法,该方法处理对输入值的更改并相应地设置状态:

onInputChange(event) {
  this.setState({ text: event.target.value });
}

Finally, in the render function, your input field would look like this (plus whatever other props you want to give it): 最后,在render函数中,您的输入字段将如下所示(加上您想要提供的其他其他道具):

<input
  value={ this.state.text }
  onChange={ this.onInputChange }
/>

As for resources, the React documentation is pretty fantastic and I found Stephen Grider's course on udemy to be the best tutorial on React: https://www.udemy.com/react-redux/learn/#/ . 至于资源,React文档非常棒,我发现Stephen Grider的udemy课程是React上最好的教程: https ://www.udemy.com/react-redux/learn/#/。 Just be sure to understand the fundamentals of React (state and props) before moving on to the Redux part of the tutorial - where things get really fun, but definitely more complex. 在进入本教程的Redux部分之前,一定要确保了解React的基础知识(状态和道具)-那里的事情真的很有趣,但肯定更复杂。

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

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