Search code examples
javascriptreactjssetstate

How to change apply setState when onChange happend?


This the code:

import React, { Component } from 'react';
import { Select } from 'antd';
import { connect } from "react-redux";

class SelecionarCrypto extends Component {
  constructor(props) {
    super(props);

    this.onChange = this.onChange.bind(this);
    this.onBlur = this.onBlur.bind(this);
    this.onFocus = this.onFocus.bind(this);
    this.onSearch = this.onSearch.bind(this);

    console.log(this.props);

    this.state = {
      ValorState: "nada"
    }


  };

 onChange(value) {
    console.log(`selected ${value}`);
    this.setState({ValorState: value});
    console.log("New value onchange", this.ValorState)

  }

  onBlur() {
    console.log('blur');
  }

  onFocus() {
    console.log('focus');
  }

  onSearch(val) {
    console.log('search:', val);
  }



render(){


const { Option } = Select;

console.log("New value Render", this.ValorState)








return (
  <Select
    showSearch
    style={{ width: 200 }}
    placeholder="Seleciona:"
    optionFilterProp="children"
    onChange={this.onChange}
    onFocus={this.onFocus}
    onBlur={this.onBlur}
    onSearch={this.onSearch}
    filterOption={(input, option) =>
      option.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
    }
  >
    <Option value="ETH">ETH</Option>
    <Option value="BTC">BTC</Option>
    <Option value="XRP">XRP</Option>
  </Select>

  );
}


  }

  const mapStateToProps = state => {
    return {
      token: state.token
    };
  };

  export default connect(mapStateToProps)(SelecionarCrypto); 

I am trying to change the value of ValorSate when onChange is done. The error I am obtaining is: TypeError: this.setState is not a function. I don´t find out the solution even readin about setSate() . I am followinf the same pattern of how-to´s or documentation but I no understanding something.

Now "New value onChange" or "New value Render" is always undefined"

console log:

enter image description here

Thank you.


Solution

  • I have modified your code. Please check it and try.

    import React, { Component } from 'react';
    import { Select } from 'antd';
    import { connect } from "react-redux";
    
    class SelecionarCrypto extends Component {
      constructor(props) {
        super(props);
    
        console.log(this.props);
    
        this.state = {
          ValorState: 'nada'
        }
      };
    
    onChange = (value) => {
      console.log(`selected ${value}`);
      this.setState({ValorState: 'algo'})
    }
    
    onBlur = () => {
      console.log('blur');
    }
    
    onFocus = () => {
      console.log('focus');
    }
    
    onSearch = (val) => {
      console.log('search:', val);
    }
    render(){
    const { Option } = Select;
    
    return (
      <Select
        showSearch
        style={{ width: 200 }}
        placeholder="Seleciona:"
        optionFilterProp="children"
        onChange={this.onChange}
        onFocus={this.onFocus}
        onBlur={this.onBlur}
        onSearch={this.onSearch}
        filterOption={(input, option) =>
          option.children.toLowerCase().indexOf(input.toLowerCase()) >= 0
        }
      >
        <Option value="ETH">ETH</Option>
        <Option value="BTC">BTC</Option>
        <Option value="XRP">XRP</Option>
      </Select>
    
      ); 
    }
    
    
    }
    
    const mapStateToProps = state => {
    return {
      token: state.token
    };
    };
    
    export default connect(mapStateToProps)(SelecionarCrypto);