0

I am working on a project which is written in react-redux. I want to use the component state in component rather than global state.
My component code is:

import React from 'react';
import Input from 'src/containers/Input';
type StateType = {
     searchChannel: string
};
export default class AddChannelComponent extends React.Component < void,
PropsType,
void > {
     state : StateType;

     constructor(props : PropsType) {
          super(props);
          this.state = {
               searchChannel: 'test'
          };
     }

     inputHandler(value) {
          console.log("text  isdddd", value);
          this.setState({searchChannel: value});  <==Error Occur Here
     }
     render() {
          return (
               <div >
                    <p>Type your input</p>
                    <div>
                         <Input inputHandler={this.inputHandler} placeholder="Search all public channels..."/>
                    </div>
               </div>
          );
     }
} 

inputHandler function is invoked by the child component. Inside this this.setState({searchChannel: value}) gives me error that this.setState is not a function. Although I assign a test value to the searchChannel in constructor and it's working when I console its value in render.

Jason Evans
  • 28,906
  • 14
  • 90
  • 154
Pulkit Aggarwal
  • 2,554
  • 4
  • 23
  • 33
  • 1
    Possible duplicate of [this.setState is undefined](https://stackoverflow.com/questions/39210971/this-setstate-is-undefined) – ivarni Aug 03 '17 at 11:14
  • 3
    TL;DR: You need to bind `inputHandler` to your component using any of the approaches suggested in the accepted answer above – ivarni Aug 03 '17 at 11:14

2 Answers2

3

My correction would be like this

import React from 'react';
import Input from 'src/containers/Input';

export default class AddChannelComponent extends React.Component{

     constructor(props) {
          super(props);
          this.state = {
               searchChannel: 'test'
          };
     }

     inputHandler = (value) => {
          console.log("text is dddd", value);
          this.setState({searchChannel: value});
     }
     render() {
          return (
               <div >
                    <p>Type your input</p>
                    <div>
                         <Input inputHandler={this.inputHandler} placeholder="Search all public channels..."/>
                    </div>
               </div>
          );
     }
} 

Reason

If you pass an instance method (which refers to a specific context aka, accessing to this) you will lose its original context, because it depends on the caller.

Further info here: How to access the correct `this` inside a callback?

eg: inputHandler={this.inputHandler}

to keep the this you can:

Hitmands
  • 13,491
  • 4
  • 34
  • 69
Hana Alaydrus
  • 2,225
  • 16
  • 19
0
import React from 'react';
import Input from 'src/containers/Input';

export default class AddChannelComponent extends React.Component{

     constructor(props) {
          super(props);
          this.state = {
               searchChannel: 'test'
          };
     }

     inputHandler = (value) => {
          console.log("text is dddd", value);
          this.setState({searchChannel: value});
     }
     render() {
          return (
               <div >
                    <p>Type your input</p>
                    <div>
                         <Input inputHandler={this.inputHandler.bind(this)} placeholder="Search all public channels..."/>
                    </div>
               </div>
          );
     }
} 
Alireza
  • 6,497
  • 13
  • 59
  • 132
SM Chinna
  • 341
  • 2
  • 7