r/reactjs • u/swyx • Feb 06 '19
Project Ideas /r/reactjs React Hooks Contest!
Make a hook or hooks app that uses one or many or ALL of the hooks APIs in a creative way!
"Creative" means whatever you want it to mean. Go nuts. Get wacky, or solve a real problem. Make the most obfuscated hook in the world. Make 3D animation. Make 2D animation. Mix it up with dynamic time intervals. Make dev-only hooks. Time Travel. Reinvent Redux. Make one that takes a lot of effort just to say "Rube Goldberg". Take https://usehooks.com/ for inspiration.
Share the Hook in a gist or demo in a Codepen or Codesandbox. Hooks you've made while in alpha are fair game, but it should be by you.
Winner gets gold and bragging rights.
•
Feb 06 '19
Still on one of the alphas but here is virtual scrolling with hooks https://react-virtual-grid.netlify.com
•
Feb 06 '19 edited Feb 06 '19
I'd thow in a masonry grid i've made, i think it demonstrates the power of hooks really well: https://codesandbox.io/embed/26mjowzpr
It uses 5 hooks that all feed into one another. It stores a bunch of items (useState), reads out media queries to make columns responsive (useMedia), measures the view width (useMeasure), then calculates the grid using that information and in a last step turns these calculations into animations (useTransition). It shuffles the whole thing with an interval (useEffect).
And my second pick would be: https://codesandbox.io/embed/j0y0vpz59
It combines a gesture hook (useGesture) with bunch of springs attached to playing cards, so that you can flick them away (useSprings).
→ More replies (0)
•
u/MrLeebo Feb 08 '19
RAT SLAYER - A simple rat-slaying game built with useReducer https://codesandbox.io/s/n4w14z0xjp
→ More replies (0)
•
u/bitttttten Feb 11 '19
i made the 2d rubiks cube using hooks: https://codesandbox.io/s/oxp916o685
it's terribly unoptimised but it was fun :D
my highest score is 62. so if you are able to beat it, let me know!
→ More replies (0)
•
u/TheNiXXeD Feb 10 '19 edited Feb 10 '19
Converted one of my tiny side projects to use hooks today. Pretty quick job in general. It's a scoring helper app for a board game called Food Chain Magnate. Also has other utility things for the game in it as well.
https://nixxed.github.io/dinnertime
I went into it expecting a lot less lines of code. Forget where I read that part. But it didn't do that, since most of my code was render anyway. But it is a lot cleaner to read now. There's part that uses local storage so got to play with an effect as well. Also one part that uses masonry. Nothing super fancy though really.
•
•
u/3_alves Feb 09 '19
Keypress custom hook used for tracking key down/up event listeners.
Hooks really are on 🔥
•
u/swizec Feb 07 '19
Here's a cool one from a while ago: https://d3blackbox.com/
`useD3` lets you give rendering control of a node to just about any JavaScript function. You could even use it to render a Vue app.
→ More replies (0)
•
u/latviancoder Feb 06 '19
Nothing too crazy, just real world usage:
https://my-cycling-routes.netlify.com/
useMemo for memoizing heavy d3 computations
useReducer together with context for state management
useContext for reading from context
custom useFetcher hook for fetching stuff (super naive though)
multiple useRef.current for holding values
useEffect for working with leaflet map and d3 graph
→ More replies (0)
•
u/pomber Feb 06 '19
I need to finish my useRender hook sometime: https://codesandbox.io/s/mm79mz1y09
→ More replies (0)
•
u/yurkaninryan Feb 06 '19
Shared this before, now that hooks are out it might be more interesting?
Beware: Probably huge anti-patterns ahead, purely for science
At my job we have a ton of components that set up logic using componentDidMount/componentDidUpdate, and expose values by taking children as a function and calling it.
My dream is to refactor them all to use hooks, but I also had the idea that if I could do something similar to useBehaviorFrom(<MouseTracker />)
that it would make adoption a bit smoother
Would anyone else have a useful for this? Terrible/Great/Meh idea? Here's a working sandbox:
→ More replies (0)
•
Feb 06 '19
Made a typing game that mimics a global store and reducer system similar to Redux. I'll be giving a hooks demonstration for a local JS meetup and wanted to create something that would assist me in my talk.
Test your WPM! See if you can get through without any errors.
Game: https://hooked-on-typing.now.sh/ Source code: https://github.com/grantsru/hooked-on-typing
•
u/solkimicreb Feb 06 '19
Made a quick thing just for this comment wall: https://codesandbox.io/s/1yqjz05xyq?view=preview It is a `useParam` hook, which works similarly to `useState` but also synchronizes the value with the URL search params. Useful for updating the URL params on data (not page) changes. I usually find triggering a whole routing in these cases an overkill. It is just a fun demo, please don't use it in any prod code. If someone wants to turn this into a lib, go ahead.
→ More replies (0)
•
u/dtcarrot Feb 07 '19
Did this a long way back when hooks were in Alpha, it's a food-themed memory card game! https://codesandbox.io/s/9ynno85v4w.
•
u/bodowota Feb 07 '19
Made this fortune wheel spinning landing page for my app: https://spinthewheel.app
For the wheel itself I am using useEffect, useLayoutEffect, useRef for handling mouse/touch events.
Lots of elements in the website use a color theme depending on how far the website is scrolled and for that I am using useReducer and useContext.
Obviously since not using any classes the website uses the useState to keep state.
→ More replies (0)
•
u/swyx Feb 06 '19 edited Feb 06 '19
presenting... useCreateClass: https://codesandbox.io/s/j2z6q89zq3
const SomeMixin = {
doSomething() {
console.log("hello from Mixin");
}
};
function App() {
const Component = useCreateClass({
statics: {
customMethod(foo) {
return foo === "bar";
}
},
mixins: [SomeMixin],
getDefaultProps() {
return {
name: "React"
};
},
getInitialState() {
return { count: 0 };
},
handleClick() {
console.log(this.state, this.setState);
this.doSomething();
this.setState({ count: this.state.count + 1 });
},
componentWillMount() {
console.log("hello from cWM");
this.setState({ count: 42 });
},
componentDidMount() {
console.log("hello from cDM");
this.setState({ count: 69 });
},
componentWillUpdate(nextProps, nextState) {
console.log("hello from cWU");
},
componentDidUpdate(prevProps, prevState) {
console.log("hello from cDU", prevProps, prevState);
// setstate causes infinite loop, avoid
},
componentWillUnmount() {
console.log("hello from cWUnmount");
},
// // todo
// componentWillReceiveProps(nextProps) {
// this.setState({ msg: nextProps.msg + "bar" });
// },
// todo
shouldComponentUpdate(nextProps, nextState) {
return nextProps.id !== this.props.id;
},
render() {
console.log(this.handleClick);
return (
<div>
<p>
hello {this.props.name}, the count is {this.state.count}
</p>
<button onClick={() => this.handleClick()}>Click Me</button>
</div>
);
}
});
console.log("hello from static", Component.customMethod("bar"));
return (
<div className="App">
<h2>createClass is coming back!</h2>
<Component name="swyx" />
</div>
);
}i cant figure out how to do cWRP and i'm pretty sure i didnt do sCU properly (should handle props) but im tired... if u wanna fork it be my guest
→ More replies (0)
•
•
u/MrLeebo Feb 07 '19
I doubt I'm the first to do it, but here's Conway's Game of Life as a react hook: https://codesandbox.io/s/6xm5zkwrwz
→ More replies (0)
0
•
u/mark_volkmann Feb 06 '19
I have two that I think are great! Both are related to state management. https://www.npmjs.com/package/context-easy and https://www.npmjs.com/package/top-state-hook
•
u/averageFlux Feb 06 '19
I have reduced my RenderBlocker class component:
class RenderBlocker extends React.Component {
shouldComponentUpdate() {
return false
}
render() {
return this.props.children
}
}to the following ridiculous one liner:
const RenderBlocker = ({ children }) => useState(children)[0]Don't know if that counts, but hey had some fun with hooks
→ More replies (0)
•
u/stinodes Feb 06 '19 edited Feb 08 '19
edit: did a big fat clean-up, think I can show this (due to iframe limitations, use the pop-up browser rather than the built-in one) edit2: made another sandbox that illustrates it a bit better. One video is your local webcam stream. The other one is your imaginary friend to whom your webcam is streamed. Added a shady emitter thing to communicate between both apps, don't hate. Actually surprised it works (again, open in pop-up browser).
I made a (pretty bad, but working) hook that allowed you to voice-call/stream with yourself (didn't develop more than that), a bit after hooks came out.
Was pretty cool, but 100% sure it's badly written and I should revisit it and 0% sure it's actually useful.
(It's the coolest thing I've done with hooks, not the best or most useful thing)
→ More replies (0)
•
u/TheNiXXeD Feb 10 '19
Didn't stop with my last project, converted another (bigger) side project over to hooks today. This one was a lot more interesting. Had to make use of useContext and useMemo as well, so I'm glad I stuck through it! I still have to convert my context providers over, but I'm almost there. Not sure if it was the latest react or CRA version, but it cut my bundle size by HALF.
This project is an alternate "forum" client for the Shacknews.com chatty. Originally wrote it in angular1, converted to 2, then to React with redux, then to React with context, now with Hooks. This one has been a good practice bed for me and web tech.
https://github.com/latestchatty/chatty