Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing url does not update websocket url #46

Open
sbland opened this issue Jul 18, 2019 · 5 comments
Open

Changing url does not update websocket url #46

sbland opened this issue Jul 18, 2019 · 5 comments

Comments

@sbland
Copy link

sbland commented Jul 18, 2019

Changing the url after component has mounted does not cause the websocket to update

Example:

const WebsocketDemo = ({ url }) => {
  const [message, setmessage] = useState('');
  const [urlVal, seturlVal] = useState(url);
  const handleData = (data) => {
    setmessage(data);
  };

  return (
    <div>
      <p>
        <input type="text" value={urlVal} onChange={(e) => { seturlVal(e.target.value) }} />
      </p>

      <Websocket
        url={urlVal}
        onMessage={handleData}
      />
    </div>
  );
};
@LeonWuV
Copy link

LeonWuV commented Nov 22, 2019

I have same problem!!!

@zqxb
Copy link

zqxb commented Mar 2, 2020

最后解决了么

@SaeedPoureshghi
Copy link

same problem!

@SaeedPoureshghi
Copy link

suddenly i found the answer : after you update the url call this.refWebSoket.sendMessage('any');
this will reset the component. dont forget that define this.refWebSoket in component as defined in example file.

@Molly6943
Copy link

Molly6943 commented Jul 17, 2020

check url changes during the life cycle of getDerivedStateFromProps

static getDerivedStateFromProps(nextProps, preState) {
    if (nextProps.url === preState.url) {
      return null
    }
    return {
      url: nextProps.url,
      ws: window.WebSocket
        ? new window.WebSocket(nextProps.url, nextProps.protocol)
        : new window.MozWebSocket(nextProps, nextProps.protocol)
    }
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants