【问题标题】:React + Material UI - Textfield "onChange" never firedReact + Material UI - 文本字段“onChange”从未触发
【发布时间】:2017-09-06 12:10:51
【问题描述】:

我尝试在我的 Textfield 被填充时触发 onchange 函数,但我无法弄清楚为什么这个函数从未被触发,即使 Chrome 的 React devtool 插件实际上触发了更改,有什么建议吗?

import React, {Component} from 'react';
import {Tracker} from 'meteor/tracker';
import {Meteor} from 'meteor/meteor';
import {Links} from '../api/links';
import LinkListItem from './LinkListItem';
import {Session} from 'meteor/session';
import SearchLink from './SearchLink';
import Fuse from 'fuse.js';

export default class LinkList extends Component {
    constructor(props) {
        super(props);
        this.state = {
            links: [],
            inputValue: ''
        };
    }
    componentDidMount() {
        this.linksTracker = Tracker.autorun(() => {
            Meteor.subscribe('links');

            const links = Links.find({visible: 
            Session.get('showVisible')}).fetch();
            this.setState({links});
        });
    }
    componentWillUnmount() {
        this.linksTracker.stop();
    }
    renderLinksListItems() {
        if (this.state.links.length === 0) {
                return (
                    <div>
                        <h2 className="link">{Session.get('showVisible') ? 'No links found' : 'No hidden links found'}</h2>
                    </div>
                );
        }
        console.log(this.state.links);
        return this.state.links.map((link) => {
            const shortUrl = Meteor.absoluteUrl(link._id);
            return <LinkListItem key={link._id} shortUrl={shortUrl} {...link}/>;
        });
    }
    _onChange(e) {
        if(e.target.value === "") {
            return;
        }
        var fuse = new Fuse(this.state.links, { keys: ["url"]});
        var result = fuse.search(e.target.value);
        this.setState({
            inputValue: e.target.value,
            links: result
        });
    }
    render() {
        return (
            <div>
              <div>
                  <SearchLink onChange={this._onChange} value={this.state.inputValue}/>
              </div>
              <div>{this.renderLinksListItems()}</div>
            </div>
        );
    }
}

我的文本域组件:

import React from 'react';
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import getMuiTheme from 'material-ui/styles/getMuiTheme';
import TextField from 'material-ui/TextField';

const muiTheme = getMuiTheme({
  palette: {
      primary1Color: '#ef6c00'
  }
})

const SearchLink = () => (
  <MuiThemeProvider muiTheme={muiTheme}>
    <TextField floatingLabelText="Search a Link" name="searchLink" fullWidth={true}/>
  </MuiThemeProvider>
);

export default SearchLink;

感谢您的帮助!

【问题讨论】:

    标签: javascript reactjs meteor onchange material-ui


    【解决方案1】:

    做这些改变:

    1. 绑定Parent组件LinkList中的方法,因为你在onChange方法里面使用this.setState,如果不bind它会抛出错误, bind 是这样的:

    <SearchLink onChange={this._onChange.bind(this)} value={this.state.inputValue}/>
    

    或在constructor 中定义binding

    2.您在props 中传递eventvalue,因此您需要在TextField 中定义这些值,如下所示:

    const SearchLink = (props) => (
        <MuiThemeProvider muiTheme={muiTheme}>
            <TextField 
                 onChange = {props.onChange} 
                 value = {props.value} 
                 floatingLabelText = "Search a Link" 
                 name = "searchLink" 
                 fullWidth = {true}/>
        </MuiThemeProvider>
    );
    

    【讨论】:

    • _onChange 已经绑定在 Linklist 类的构造函数方法中,我只是忘记复制/粘贴这一特定行,我已经按照您的建议进行了更改,但出现以下错误: Uncaught TypeError: Cannot read property 'value' of undefined in &lt;TextField onChange={(event, value) =&gt; this.props._onChange(event, value)} value={this.props.value} floatingLabelText="Search a Link" name="searchLink" fullWidth={true}/&gt;
    • 查看代码,我没有使用this.props.vaule,使用props.value,它的功能组件this不会出现。
    • 我已经考虑到你刚刚所做的修改,但现在我有这个错误:SearchLink.js:14 Uncaught TypeError: props._onChange is not a function
    • 你使用了额外的_,删除它,使用props.onChange,使用它可以工作的确切代码:)
    • 你确实用过“this”,你刚刚修改了你的第一条评论。
    猜你喜欢
    • 1970-01-01
    • 2019-06-12
    • 2021-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 1970-01-01
    相关资源
    最近更新 更多