Kentron.dna
Kentron.dna

Reputation: 193

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.

Upvotes: 0

Views: 293

Answers (4)

cr kushal
cr kushal

Reputation: 11

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

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

    //this.onChange = this.onChange.bind(this);

    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);

Upvotes: 0

Brijesh Dhanani
Brijesh Dhanani

Reputation: 886

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);

Upvotes: 1

Anurag Srivastava
Anurag Srivastava

Reputation: 14423

Move those functions outside the render, bind them to the this of your component and reference them with the this keyword:

class SelecionarCrypto extends Component {
  constructor(props) {
     ...
     this.onChange = this.onChange.bind(this)
     // Similar for the rest  
  }

  onChange(value) { this.setState({ ValorState: value }) }
  onBlur() {}
  onFocus() {}
  onSearch() {}

  ...

  render(){

  ...
  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>
  )
}

Upvotes: 1

arslan
arslan

Reputation: 1144

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);

    console.log(this.props);

    this.state = {
      ValorState: 'nada'
    }


  };

 onChange=(value)=> {
  console.log(`selected ${value}`);
  this.setState({ValorState: 'algo'})
}

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

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

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



render(){


const { Option } = Select;



return (
  <Select
    showSearch
    style={{ width: 200 }}
    placeholder="Seleciona:"
    optionFilterProp="children"
    onChange={this.onChange}
    onFocus={onFocus}
    onBlur={onBlur}
    onSearch={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); 

function must be a outside from render and bind onchange function otherwise setstate will not effective

Upvotes: 0

Related Questions