jdog
jdog

Reputation: 10759

Why is my setState function not undefined?

Trying to use a setState function and no matter where I use it, it is coming back as undefined?

const StreamingCaptionViewer = () => {
  const [trans, setTrans] = useState(null);
  const [sentidata, setSentidata] = useState();
  const [entidata, setEntidata] = useState();
  const socket = useSocket('transcript', (data) => {
    console.log(`data recieved: ${JSON.stringify(data)}`);
    setTrans(data);
  });

  this.setSentidata = this.setSentidata.bind(this);  // Here is the error (undefined)

  const ErrorMessage = {
    color: 'red',
    fontSize: '13px',
    visibility: 'hidden',
  };

Upvotes: 0

Views: 56

Answers (2)

madmax
madmax

Reputation: 1

If it is a functional component, you have access to the hook directly inside the function.

You dont need to bind the hook, i.e. , you need not write: this.setSentidata = this.setSentidata.bind(this);

Your can directly use setSentidata(what you want to set sentidata to)

Upvotes: 0

imjared
imjared

Reputation: 20564

You're not using a class component so there's no need for this. You can simply setSentidata(whatever)

Upvotes: 2

Related Questions