简体   繁体   English

重用不同状态的组件如何反应

[英]How reuse react component with different states

I'm creating a list that shows diferents rows with different inputs.我正在创建一个列表,显示具有不同输入的不同行。 The diferents inputs are conditioned to the selected, but my problem is that they change in all rows... and I do not know how to do it well.不同的输入取决于所选,但我的问题是它们在所有行中都发生了变化......我不知道如何做好。

An example:一个例子:

I have a parent element (orange) and depending on the selected in the first select element, the child change (blue).我有一个父元素(橙色),根据第一个选择元素中的选择,子元素会发生变化(蓝色)。

Parent and child element screenshot父子元素截图

But the problem is that when I select one option, all the children of the diferent rows changes (blue).但问题是,当我选择一个选项时,不同行的所有子项都会更改(蓝色)。

The problem screenshot问题截图

Here, the parent component code:这里,父组件代码:

class FilterByDateCondition extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            dateTypeCondition: "calendar",
        };
    }

    componentDidMount() {
        $('.date-condition-select').select2({
            width: '100%',
                minimumResultsForSearch: Infinity,
        })

        const s = this;
        $('.date-type-condition-select').select2({
            width: '100%',
            minimumResultsForSearch: Infinity,
        }).on("select2:select", function (e) {
            let state = s.state;
            state.dateTypeCondition = $(this).val();
            s.setState(state);
        });
    }

    _drawDateTypeCondition(dateTypeCondition) {
        if (dateTypeCondition == "calendar") {
            return <DateCalendar/>
        }
        else if (dateTypeCondition == "expression") {
            return <DateExpression/>
        }
    }

    render() {
        return (<div className="row">
                <div className="col-md-3">
                    <select className="date-type-condition-select">
                        <option value="calendar">{gettext("Calendar")}    </option>    
                        <option value="expression">{gettext("Expression")}</option>
                    </select>
                </div>
                <div className="col-md-2">
                    <select className="date-condition-select">
                        <option value="<">{"<"}</option>
                        <option value=">">{">"}</option>
                        <option value="==">{"=="}</option>
                    </select>
                </div>
                <div className="col-md-7">
                    {this._drawDateTypeCondition(this.state.dateTypeCondition)}
                </div>
            </div>
        );
    }
}

The chidren component are shown in the function _drawDateTypeCondition _drawDateTypeCondition组件显示在函数_drawDateTypeCondition

How to reuse the components??如何重用组件?

Thank you very much!!!非常感谢!!! : ] :]

I think this has to do with the class you are using.我认为这与您正在使用的课程有关。 The select tags in both the parent components have the same class and since you are using class selector for trigering an action in you componentDidMount function, it doesn't matter which select is selected as long as it has that class.两个父组件中的选择标记具有相同的类,并且由于您使用类选择器来触发componentDidMount函数中的操作,因此只要具有该类,选择哪个选择并不重要。

I think a better way about this would be using a different identifier or adding a onClick or onSelect event to the select tags which trigger a seperately defined member function.我认为更好的方法是使用不同的标识符或将 onClick 或 onSelect 事件添加到触发单独定义的成员函数的选择标签中。

I hope this makes sense.我希望这是有道理的。

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

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