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

Can We Use Reducers to Manage Complex State Across All Pages, Particularly MarketDetails? #103

Open
pwdel opened this issue Feb 28, 2024 · 1 comment

Comments

@pwdel
Copy link
Member

pwdel commented Feb 28, 2024

MarketDetails.js as a whole has several pieces of complex state involved.

State includes:

market - Stores the market data fetched from the API. This is essential state.
currentProbability - Stores the latest probability to display.
chartData - Stores data for the probability chart.
numUsers - Number of users who bet on the market. Essential state.
totalVolume - Total amount bet on the market. Essential state.
creator - Information about the market creator.
betAmount - Stores user's bet amount. Essential for betting form.
showBetModal - Controls bet modal visibility. Essential for modal.
selectedOutcome - Selected bet outcome. Essential for betting form.
bets - List of bets on the market. Essential state.
isActivityModalOpen - Controls activity modal visibility. Essential for modal.
activeTab - Currently selected tab in activity modal. Essential for modal.
showResolveModal - Controls resolve modal visibility. Essential for modal.
selectedResolution - Selected resolution for market. Essential for resolving.
resolutionPercentage - Percentage resolution. Essential for resolving.

Derive currentProbability, chartData, creator from market data instead of separate state
Move modal state (isActivityModalOpen, activeTab, showBetModal, showResolveModal) into separate ModalManager component
Use a reducer for managing modal state since there are multiple related states
Move bet amount state (betAmount, selectedOutcome) into separate BetForm component

Other ideas...

Can we use reducer functions to reduce complexity?

A reducer function accepts two parameters:

  1. an objet or array that represents a given state.
  2. an action that describes how you want to modify the state

The function returns a new copy of the state we pass to it.

  • the action parameter can be whatever or an object with a string type attribute and a payload with additional information.

Examples shown from O'Reilly React Cookbook:

https://github.com/dogriffiths/ReactCookbook-source/tree/master/ch04-03-reducer-seq/src

@pwdel
Copy link
Member Author

pwdel commented Feb 28, 2024

Feedback from @markokovac16 :

  1. Separate as much of everything into utils so your components are not behemoths
  2. A part of the reason they're so huge because you always check for tokens/username/whatever, every piece of data can be a part of the global state, React Query is perfect for this usecase as it comes in with a lot of built-in stuff, it reduces the network load and loading time because it caches data so it doesn't constantly fetch the same data
    That's something that's going to be hugely important considering you plan on a big user-base.

As soon as you're in a position when you pass props to every single component, you should consider a state manager

Especially if you pass the props down several levels, it makes it super hard to maintain and debug

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

No branches or pull requests

1 participant