1

我是 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()

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

此致

4

1 回答 1

0

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

于 2016-06-22T21:36:04.990 回答