【问题标题】:Pass value from class method to a react component将值从类方法传递给反应组件
【发布时间】:2018-11-01 03:00:22
【问题描述】:

情况:我有一个类有自己的方法。此类在 React 组件中实例化。

我需要什么:在类中的一个方法中,它使用 .val() 更改了输入 (this.$el) 的值,但我正在听取更改通过onChange 在 React.component 中输入。我需要将用于设置输入值的值(通过this.$el.val(value))传递给 React 组件以更改其状态。

我的尝试:我尝试将.change() 和trigger('change') 链接到val(value),但没有任何影响。

所以,当在类方法中设置时,我需要能够访问我在 React 组件中 .val(value) 中使用的值。我考虑过使用一个方法并在componentWillUpdate 上调用该方法,但是组件没有更新,因为通过val() 设置输入值不会触发更改。

有什么想法吗?

组件代码:

// Create a ref to manage blur/focus state on the search input
this.inputRef = React.createRef()
// Setup initial state
this.state = {
  supersearchResults: [],
  value: this.props.initialValue || ''
}
this.onInputChange = this.onInputChange.bind(this)

tag('input', {
      autoComplete: 'off',
      className: blockElement('search-input'),
      onChange: this.onInputChange,
      placeholder: 'Find people, collections and pages',
      ref: this.inputRef,
      type: 'text',
      value: this.state.value
 })

类代码: this = 班级 this.$el = 输入

// What is happening:
// A user types in an input, suggestions display in a list, when you
// select a suggestion, it calls the below to change the input value
this.$el.val(complete)
this.$el.blur()
this.hide()

【问题讨论】:

  • 能多加点代码理解吗?
  • 添加了更多。应该够了

标签: javascript jquery reactjs class data-binding


【解决方案1】:

如果我理解正确,您希望能够访问 html 字段的值。那么请考虑以下注意事项。使用受控输入,使得

class ReactComponent extends...
    constuctor (props) {
        super();
        this.state = { fieldValue: props.fieldValue || '' };
    }

    onFieldChange = (event) => {
       this.setState('fieldValue': event.currentTarget.value)
    }

    render () {
        return (
           <div>
                <input type="text"
                    value={this.state.fieldValue}
                    onChange={this.onFieldChange}
                >
           <div>
        )
    }
}

现在有了这段代码,如果你需要使用一些外部类来调用一些代码,只需将它正确地放入生命周期中。但是为了引用该值,请使用组件状态。如果您想以编程方式更改值,请执行相同的更新状态中的值。如果我错过了什么,请在 cmets 中告诉我。

【讨论】:

    【解决方案2】:

    您需要将状态保留在您的类组件中。考虑以下

    class TextExample extends Component{
      constructor(){
        super(props);
        this.state ={
          username: null
        }
        this._handleChange = this._handleChange.bind(this);
      }
    
      _handleChange(e){
        const { name, value } = e.target;
        this.setState({ username: value}) // for single TextField
        // if you want to reuse this _handleChange function for all the TextFields then you need to use the below commented code which updates state of current TextField
    
       //this.setState({ [name]: value })  // it is similar like the JSON bracket notation 
      }
    
      render(){
        return(
          <div>
            <TextField
              id="username"
              label="Username"
              name="username"
              value={this.state.username}
              onChange={this._handleChange}   // it will call the _handleChange function on every keypress inside the TextField. 
            />
          </div>
        )
      }
    }
    

    【讨论】:

    • 我确实有这个,但是因为当类方法调用.val() 时onChange 没有被触发,所以它没有设置。我希望将传递给.val() 的值传递给组件以使用它设置状态。
    • 你能展示你的类组件吗?这对我们有帮助
    猜你喜欢
    • 2021-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-20
    • 2018-11-12
    • 1970-01-01
    • 2019-01-02
    • 1970-01-01
    相关资源
    最近更新 更多