On another note, most of these complaints, IMO, aren't even specific to React. We were barreling down this future during the days of jQuery.
On another note, most of these complaints, IMO, aren't even specific to React. We were barreling down this future during the days of jQuery.
ReactJS has a wildly inflexible architecture that aggressively forces their users to conform to an orthodoxy. As an example let's say we have the following code
function C() {
var A = {
something: () => console.log("hi")
},
B = document.createElement('div');
B.onclick = () => A.something();
}
This small snippet of code violates "best practices" in react and thus is almost, but not quite impossible to do.See the problem here is that B and A are directly interacting with each other. If we were to map things in react-land, they would show that C has "children" of A and B and here A and B talk to each other "horizontally" which is heretic to the purity of the orthodoxy.
The hierarchical nature here is an unwavering assertion about the world. Essentially the claim is as follows:
C
/ \
A---B
The A <=> B is what forces us to wear a scarlet letter and live on the edge of the town.Therefore we no longer solve a problem. Instead, we are imposed upon us an aesthetic puritism do to a natural belief of "props down, events up". A popular way to do this basic programming technique that's been readily available in effectively every programming language since the first digital computer (Plankalkül had it in 1942) is the usual suspects either add "a new testament" to the orthodoxy, in this case with something called "redux" or figure out an acrobatic way to comply.
Let's do the second. So we need to "pass down" things that will "pass up". We have a solution, now we must go backwards, first we will go into "classes" to make things more orthodox.
class A {
componentDidMount() {
this.props.connector(this);
}
something() {
console.log('hi');
}
render() {
return;
}
};
class B {
render() {
return <div onClick={ this.props.onClick }>
}
};
class C {
constructor() {
super(props);
// here's our connecting function
this.connector = this.connector.bind(this);
this.onclick = this.onclick.bind(this);
this.state = {connector: ''};
}
connector(obj) {
this.setState({connector: obj});
}
onclick() {
this.state.connector.something();
}
render() {
return (
<A connector={ this.connector } />
<B onClick={ this.onclick } />
);
}
};
So to simplify this flow A -> B
We now do C -> A -> C -> B -> C -> A
Or as a diagram, we get the following: .---◢ ◣----.
.▶B---▶C--. \
| ◤--'|\ ▼ /
\____/ '►A-'
See how we do multiple round trips passing things through C in order to maintain the hierarchy? This is how we stay within the orthodoxyIf this looks like Ptolemaic astonomy, that's not a coincidence! In Ptolemaic astronomy the assumption of Earth being the center of the universe was enforced as the orthodoxy. As a result you got epicycles upon epicycles because the simpler model was forbidden.
People were so committed to the more convoluted orthodoxy that anyone who questioned it was put on trial for heresy.
Claiming that A -> B was even possible was the crime.
The great success of React is in enforcing a new kind of authoritarianism: it enforces doctrine by explicitly disallowing heretical code.
A problem that I have never seen before, an extremely complicated description, a terrible implementation in react using class components, some weird circular diagrams.
This looks like it's generated by GPT-3.
What is this piece of code even supposed to do?
function C() {
var A = {
something: () => console.log("hi")
},
B = document.createElement('div');
B.onclick = () => A.something();
}
In react: function C() {
const A = {
something: () => console.log("hi")
};
return <div onClick={A.something} />
}I'm glad you disagree btw. If everyone agreed with me than honestly, I'd not be pushing hard enough.
"If reality falls outside the doctrine and really can't be denied then instead of fixing the assumption more often the orthodoxy is amended. A new wing in politics, a new testament in religion, a new movement in the arts, and a new version in software. It permits the avoidance of admitting the failures of orthodoxy while permitting a mechanism to address its shortcomings."
I don't think we'll come to an agreement on this and that's fine, it means I am staking a clear and controversial position which is my goal. Honest people can disagree and I really do appreciate your honesty. Thanks
Isn't it possible to connect your abstract ideas to a real world example? And why create en extremely obfuscated react class implementation of something that can be done in 3 lines of functional react code?
Let's say you have things with locations and a map (scooters, restaurants, hidden gold, whatever). You click a thing, it highlights it on the map. You click the map, it highlights a thing.
The obvious to me way of doing this is singletons (or globals).
You've got some routine itemhighlight(thing) or mapcenter(thing) and you generate the things on the page and use something simple like html data attributes to pass things around.
In react there's a fairly unorthodox hack I've got to do things like this but normally you'd have a map component and then some list component with item components.
Then when you tap the map, the stuff is passed up to a container component that gets passed to the list component that goes back to the item component, so long as you create some connections for that to happen.
Everything goes up and down the hierarchy. You don't just directly talk horizontally.
Now sure, there's arguments for and against both approaches and they are as contentious as type systems and editors. But the question was about the "scolding nanny".
Without react you can do both approaches, the first of which requires competency and skill to do well without creating a mess sure. But in react you are precluded from doing the first unless you wrangle it and work around the framework.
It disciplines and limits your freedom. I understand corporate hierarchies, pecking orders, and reasons for doing this. I'm a true anarchist though and that limitation really pushes my buttons.
Given all that, I use react regardless because I first and foremost try to be responsible adult and think it's the current most popular and professional way to do things for the day I'll eventually move on and hand over my code to someone else.
But I fucking hate it. It's a seething and very unhealthy vitriolic hate.
If you want to go for a sane way of managing global state you can use redux or context which don't require you passing state up and down the component hierarchy. But in my experience, usually when you design your hierarchy and your data well it makes most sense actually using the hierarchy to your advantage.
There's something fractal about it where the higher-level components deal with higher-level state and problems. In the case of the map you have a shared state between the map and the controls which should be explicit in my opinion and not hidden in one of the components' html data attributes.
The more complex applications I develop, the more appreciate the concept of having a "single source of truth" and not sprinkling states in child components.
So instead of the direct path, now you are doing a much more complicated circuitous path because the orthodoxy had to be amended.
Again, you're going to say this is good and I'm going to say it's bad. I see a problem where you see a solution. This is not reconcilable, that's why there's so many different programming languages. There's no right answer here and I'm ok with merely having a collection of opinions. It's like politics and religion. Whatever, people are different.
A shopping cart. The cart state exists in the header of the web page, but the "add to cart" button exists on the product description, far away from the cart. Ultimately, I think, it ends up being a question about state management which is always the hardest part of UI frameworks IMO
These days you can do it with a useState or useReducer hook but there are countless ways to implement it, react or not.
Horizontal message passing is really a state related issue and lives outside of React.
You can manage your state however you want and then wire up the components however you want. Since those two things are separate concerns. Although there are a ton of options in the ecosystem to do that.
const B = ({onClick}) => <div onClick={onClick} />;
function C() {
const A = {
something: () => console.log("hi")
};
return <B onClick={() => A.something()} />;
}That said,
>The great success of React is in enforcing a new kind of authoritarianism: it enforces doctrine by explicitly disallowing heretical code.
I don't think this is a bad thing, after all the success of Rust is also predicated on explicitly disallowing heretical code that may have been commonplace in C. Constraints lead to more maintainable code, which is ultimately what frameworks these provide. I don't think your complaint is unique to React due to the fact it's HTML that influences that hierarchal design. That said, I'm not a front-end dev, and I my exposure to UI patterns is relatively limited; so I'm looking forward to your eventual book.
Button Press -> State -> Shopping Cart View
Just have the state have a function for dispatching a change to the shopping cart which is passed to the button component and pass the actual state to the shopping cart view.
You can have the state live in a parent component, use redux, use context or go more hacky.
My initial reading of OP's post is that this is the "hack" he doesn't like (even Context wasn't all that encouraged in React IIRC). My amusement, however, is this is, to him, a fundamentally philosophical disagreement with the design of React