You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Jun 3, 2022. It is now read-only.
I'm leaving my architecture notes from #8 in this issue, but it could probably be broken into smaller issues.
In React, components should be small/single responsibility and only affect themselves (through configuration, their children).
For instance, DOMController currently does a bunch of things:
Draws the UI scaffolding
Determines which route to show
Toggles the visibility of its parent element
Sets up keybindings
These could be broken into separate elements:
classRemixerRouterextendsReact.Component{state={showOptions: false,}// Use an arrow function to get correct `this` bindingtoggleOptions=()=>{this.setState({showOptions: !this.state.showOptions,});}render(){return(<RemixerPaneltoggleOptions={this.toggleOptions}>{this.state.showOptions
? <Options/>
: <VariableConfigurator/>}</RemixerPanel>)}}functionRemixerPanel(props){return(<divclassName="rmx-card-wide mdl-card mdl-shadow--6dp"><divclassName="mdl-card__title"><h2className="mdl-card__title-text">
Remixer
</h2></div><divclassName="mdl-card__menu"><buttonclassName="mdl-button mdl-button--icon mdl-js-button mdl-js-ripple-effect"onClick={props.toggleOptions}><iclassName="material-icons">
menu
</i></button></div><divclassName="mdl-card__supporting-text mdl-card__actions mdl-card--border">{children}</div></div>);}functionCloak(props){returnprops.hide
? null
: props.children;}
I'm going to move the global event listening out of a component and into the top level. This is a common pattern in React: to have all your shared state at the top-level, passed down via props.
For such a simple example, this is probably fine. However, as the app grows, you might find you want more order. That's where Redux comes in. It's this same pattern, but organized:
Redux is conceptually really simple. You have a high-level state atom (effectively just a tree of data you want to keep track of). Yours might be {showRemixer: true, showOptions: false}. Then, you have reducers that operate on that state. You might have this:
The messages you are currently sending are called "actions" in Redux parlance. Every time it receives one, it calls the reducer to get an updated state. It then passes that updated state into your component tree (Cloak in our example above) to trigger a rerender. As you can see, this is the same thing I've done in the top-level of the example, just a bit more structured.
Imagine you wanted to add a keybinding to toggle the options. You could do so with a reducer that looked like this:
RemixerPanel uses the toggleOptionsAction to handle clicks on the menu button, and your key listener calls it whenever someone hits the ? key. Redux would pass showOptions into RemixerRouter as a prop.
Redux is a 7K dependency. Since this is a developer tool, that's probably not an impactful number. For cleanliness/maintainability, it may be worth adopting.
The text was updated successfully, but these errors were encountered:
I'm leaving my architecture notes from #8 in this issue, but it could probably be broken into smaller issues.
In React, components should be small/single responsibility and only affect themselves (through configuration, their children).
For instance,
DOMController
currently does a bunch of things:These could be broken into separate elements:
I'm going to move the global event listening out of a component and into the top level. This is a common pattern in React: to have all your shared state at the top-level, passed down via props.
For such a simple example, this is probably fine. However, as the app grows, you might find you want more order. That's where Redux comes in. It's this same pattern, but organized:
Redux is conceptually really simple. You have a high-level state atom (effectively just a tree of data you want to keep track of). Yours might be
{showRemixer: true, showOptions: false}
. Then, you have reducers that operate on that state. You might have this:The messages you are currently sending are called "actions" in Redux parlance. Every time it receives one, it calls the reducer to get an updated state. It then passes that updated state into your component tree (
Cloak
in our example above) to trigger a rerender. As you can see, this is the same thing I've done in the top-level of the example, just a bit more structured.Imagine you wanted to add a keybinding to toggle the options. You could do so with a reducer that looked like this:
Your router simplifies to just this:
RemixerPanel
uses thetoggleOptionsAction
to handle clicks on the menu button, and your key listener calls it whenever someone hits the?
key. Redux would passshowOptions
intoRemixerRouter
as a prop.Redux is a 7K dependency. Since this is a developer tool, that's probably not an impactful number. For cleanliness/maintainability, it may be worth adopting.
The text was updated successfully, but these errors were encountered: