roblox-tsr
roblox-ts6mo ago
3 replies
kv_

React Context hook does not update consumer

Hello, I have yet another react question :p
I am creating a datastore editor plugin and I have a list of datastore keys. Each of these is a button and when clicked, the data associated with that key should appear in the main editor. In order to do this I am using a context hook, and each key selection is a provider for the context hook. The main editor is a consumer of the hook. The context is created outside of the main component and passed in as a prop (I am not sure how else to do this, I could create the context inside the main component but I see no difference there):

onStart(): void {
  const defaultContext = {
    keys: this._editorSession.getKeys(),
    active_key: ""
  }
  
  this._root = createRoot(this._widget);
  this._reactContext = React.createContext(defaultContext) as never; // create the default context and pass as prop
  this._root.render(React.createElement(App, { editor: this._editorSession, context: this._reactContext }));
}


continued below...
Was this page helpful?