【问题标题】:ReactJS - can not get information about inputReactJS - 无法获取有关输入的信息
【发布时间】:2016-06-22 15:20:19
【问题描述】:

我是 reactjs 的新手。我不知道如何获得输入字段的值。我向您展示我如何编写组件的最佳方式:

create class inputFields extends React.Component{ 
  getAllInformationAsJSON() {
    return {
        company : this.refs.company.getText(),
        street : this.refs.street.getText(),
        plz : this.refs.plz.getText(),
        city : this.refs.city.getText(),
        description : this.refs.description.getText(),

    }
}

render() {
    return (
        <div style={cssStyle.bodyStyle}>
            <FormComponentText ref="company"   placeholder= {Translation.Location.firmaPlaceholder}/>
            <FormComponentText ref="street" placeholder={Translation.Location.streetPlaceholder}/>
            <FormComponentText ref="plz" width='20%' placeholder={Translation.Location.PLZPlaceholder}/>
            <FormComponentText ref="city" width='80%' placeholder={Translation.Location.placePlaceholder}/>
            <FormComponentArea ref="description" rows='7' placeholder={Translation.Location.descriptionPlaceholder}/>
            <div><button onClick={this.getAllInformationAsJSON}>Test</button></div>
        </div >
    )
}
}

如果我在 html 文档中呈现它,它工作正常!

render(){
      return (<inputFields />)
}

然后,如果我调用该函数,我将获得所有信息。但我想把这个组件放在另一个组件中:

addChild(component) {

    viewTitle = Translation.Settings.User.title;
    viewConfig.view.window.width = 600;
    viewConfig.view.window.height = 400;
    *bodyInfo* = <**inputFields**  />;

    this.props.addComponentToView(<JSPanel idInfo="" config={viewConfig} title={viewTitle} bodyInfo={*bodyInfo*}/>);
}

一个JSPanel:

class JSPanel extends React.Component {

constructor(props) {
    super(props);
    this.sendInformation = this.sendInformation.bind(this);
}


sendInformation() {
    var test = findDOMNode("TEST");
    request("/componente/location", requestData.POST, '{info:test}', requestData.CONTENT_JSON)
        .then(token => {

        }).catch(err => console.log(err))
}


render() {
    return this.state.showImage ? (<div ref="ich" style={this.state.css.view}>
            <div style={this.state.css.body}>
                {this.props.**bodyInfo**}
            </div>
        </div>
    ) : (<div></div>)
}

我想要的是通过函数从输入中获取信息:sendInformation()

我希望我的意思很清楚,任何人都可以帮助我吗?

最好的问候

【问题讨论】:

  • 你是按照谁的教程来实现这个结果的?我建议你深入了解 facebook react docs
  • 这是错误的方式吗?我没有遵循一些教程。

标签: reactjs components refs


【解决方案1】:

这样做的一个好方法是在用户键入它们时将值存储在状态中https://facebook.github.io/react/docs/forms.html#controlled-components

【讨论】:

  • 好的,但是如何在 sendInformation() 函数中从组件中获取状态信息?这是我最大的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-13
  • 2018-12-25
  • 2020-04-03
  • 1970-01-01
  • 2014-05-04
相关资源
最近更新 更多