ReactJS with ES6: this.props is not a function when I communicate two components

前端 未结 3 569
天涯浪人
天涯浪人 2020-11-30 04:40

I\'m working with ReactJS with ES6, but I have some problems to communicate child > parent through props. Example of my approach:

class SearchBar extends Rea         


        
3条回答
  •  佛祖请我去吃肉
    2020-11-30 05:30

    You are missing binding in your constructor, also you don't need to pass props if you are not using them in the constructor. Also you need to import { PropTypes } from 'react'

    class SearchBar extends React.Component {
    
      constructor() {
        super();
        this.handler = this.handler.bind(this);
      }
    
      handler(e){
        this.props.filterUser(e.target.value);
      }
    
      render () {
        return (
          
    ); } } export default class User extends React.Component { constructor() { super(); this.filterUser = this.filterUser.bind(this); this.state = { name: '', age: '', filter: '' }; } filterUser(filterValue){ this.setState({ filter: filterValue }); } render() { return (
    Value: {this.state.filter}
    ); } }

提交回复
热议问题