【问题标题】:React setting and clearing state through functions通过函数反应设置和清除状态
【发布时间】:2018-01-18 03:14:27
【问题描述】:

在这里反应新手(谈论陡峭的学习曲线!)。

使用关键字输入和清除按钮构建一个简单的组件 - 但难以理解为什么我的状态值一直被记录为对象?

任何建议都会很棒 - 谢谢!

`    class FacetsFilter extends React.Component {
        constructor(props) {
            super(props);
            this.state = {
                facetsKeyword: ''
            };
        }

        handleOnChange(e) {
            this.setState({
                facetsKeyword: e
            });
            console.log('This is it : ' + e);
        }
        handleClearClick(e) {
            e.preventDefault();
            this.setState({
                facetsKeyword: ''
            });
        }

        render() {
            return (
                <div className="facetsFilter">
                    <h3 className="facetsTitle title is-5">Refine results</h3>
                    <div className="facetsKeywordFilter columns is-gapless">
                        <div className="column">
                            <TextField
                                id="facetsKeywordInput"
                                className="facetsKeywordInput"
                                hintText="Keyword filter"
                                onChange={this.handleOnChange.bind(this)}
                                value={this.state.facetsKeyword}
                            />
                        </div>
                        <div className="column is-narrow">
                            <IconButton
                                className="facetsClearButton"
                                tooltip="Clear filter"
                                onClick={this.handleClearClick.bind(this)} >
                                <NavigationClose />
                            </IconButton>
                        </div>
                    </div>

                    <div className="facetsList body-2">
                        {facetsList.map((item, index) => (
                            <div>
                                <h3 key={index} className="facetsCategoryTitle">{item.category}</h3>
                                <div className="facetListItems">
                                    {item.facets.map((subitem) => (
                                        <div>{subitem}</div>
                                    ))}
                                </div>
                            </div>
                        ))}
                    </div>
                </div>
            );
        }
    }

    export default FacetsFilter;`

我只是想知道为什么当我控制台记录它作为 [Object Object] 返回的 onChange 函数时。

感谢您的任何建议。

【问题讨论】:

    标签: reactjs state


    【解决方案1】:

    传递的参数e 不是文本而是javascript 事件对象。因此,如果您想更改文本,请使用e.target.value

        handleOnChange(e) {
            const {value} = e.target;
            this.setState({
                facetsKeyword: value
            });
            console.log('This is it : ' + value);
        }
    

    【讨论】:

    • 谢谢你,先生 - 这很有意义。
    猜你喜欢
    • 2015-08-15
    • 2018-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-20
    • 2020-11-11
    相关资源
    最近更新 更多