React + Material UI - Textfield“onChange”永远不会被触发

时间:2017-04-11 08:13:29

标签: javascript reactjs meteor onchange material-ui

当我填充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>
        );
    }
}

我的Textfield组件:

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;

感谢您的帮助!

1 个答案:

答案 0 :(得分:3)

进行以下更改:

1。绑定父组件LinkList中的方法,因为您在this.setState方法中使用onChange,如果您没有bind它会抛出错误,bind就像这样:

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

或在binding中定义constructor

2。您正在event中传递valueprops,因此您需要在TextField中定义这些值,就像这样:

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