简体   繁体   English

在React中的div中显示表单选择

[英]Display form selection in div in React

I am trying to create a UI that updates as the client selects an option from a dropdown menu. 我正在尝试创建一个UI,该UI在客户端从下拉菜单中选择一个选项时进行更新。 I've tried to set this up as follows: A react Parent ( App ) that renders two child components ( Inputs and Display ). 我尝试将其设置如下:渲染两个子组件( InputsDisplay )的react Parent( App )。 Inputs renders a dropdown menu form. Inputs呈现一个下拉菜单形式。 Display renders html output that displays the selection made in Input . Display呈现html输出,显示在Input所做的选择。

Example below: 下面的例子:

    var Inputs=React.createClass({
    getInitialState: function() {
        return {value: 'cat'}
    },
    handleChange: function() {
        this.setState({value: event.target.value})
    },
    render: function() {
        return (
            <div>
                <form>
                    <select value={this.state.value}
                        onChange={this.handleChange}>
                        <option value='cat'> Cat </option>
                        <option value='dog'> Dog </option>
                    </select>
                </form>
            </div>
        )
    }
})

var Display=React.createClass({
    render: function(){
        return (
            <div>
                <b> {this.props.l1} </b>
            </div>
        )
    }
})

var App = React.createClass({
    render: function() {
        return (
            <div>
                <Inputs ref="inputs" />
                <Display l1={this.refs.inputs.state.value} />
            </div>
        )
    }
})

ReactDOM.render(
<App />,
document.getElementById('app')
  );

I expect the value to change when the client selects a new value from the dropdown. 当客户端从下拉列表中选择新值时,我希望该值会更改。

More fundamentally, I think I'm misunderstanding how states and properties are passed in React. 从根本上来说,我认为我误解了状态和属性如何在React中传递。

Because you want both of the components to render data based on selections you should let the App do the state management and pass data as props to each one. 因为您希望两个组件都基于选择来呈现数据,所以应该让该应用执行状态管理并将数据作为道具传递给每个组件。

var App = React.createClass({
    getInitialState: function() {
        return {value: 'cat'};
    },
    handleChange: function(e) {
        e.preventDefault();
        this.setState({value: e.target.value});
    },
    render: function() {
        return (
            <div>
                <Inputs value={this.state.value} onChange={this.handleChange} />
                <Display l1={this.state.value} />
            </div>
        )
    }
});

var Inputs = React.createClass({
    render: function() {
        return (
            <div>
                <form>
                    <select value={this.props.value}
                        onChange={this.props.onChange}>
                        <option value='cat'> Cat </option>
                        <option value='dog'> Dog </option>
                    </select>
                </form>
            </div>
        )
    }
});

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

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