question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

API design question

See original GitHub issue

Hello Daishi. Trying to wrap my head around your library.

I’m curious why did you design the reducer API like this:

const initialState = { counter: 0 }; // initialState is not optional.
const { GlobalStateProvider, dispatch, useGlobalState } = createStore(reducer, initialState);

const Counter = () => {
  const [value] = useGlobalState('counter');
  return (
    <div>
      <span>Counter: {value}</span>
      <button onClick={() => dispatch({ type: 'increment' })}>+1</button>
      <button onClick={() => dispatch({ type: 'decrement' })}>-1</button>
    </div>
  );
};

and not like this:

const initialState = { counter: 0 }; // initialState is not optional.
const { GlobalStateProvider, useGlobalState } = createStore(reducer, initialState);

const Counter = () => {
  const [value, dispatch] = useGlobalState('counter');
  return (
    <div>
      <span>Counter: {value}</span>
      <button onClick={() => dispatch({ type: 'increment' })}>+1</button>
      <button onClick={() => dispatch({ type: 'decrement' })}>-1</button>
    </div>
  );
};

I’m not sure about all pros & cons, except that the second one looks more similar to basic setState option (which is a plus). Will appreciate your clarification.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:14 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
dai-shicommented, Feb 8, 2019

@jacob-ebey Yeah, my original motivation and implementation was also eliminating context, and then eventually I came back to use context which seems to work better in concurrent mode in the future. I also wanted to make use of observedBits for performance.

0reactions
dai-shicommented, Mar 17, 2019

Closing this issue. Feel free to open a new one.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Top REST API Interview Questions and Answers (2023)
REST API Basic Interview Questions · 1. What do you understand by RESTful Web Services? · 2. What is a REST Resource? ·...
Read more >
13 API Design Interview Questions to Ask Developers ... - Twine
Question 1: What is API Design? ... API Design is defining interfaces/contracts for interacting between systems (server-to-server or server-to- ...
Read more >
Answers To The Most Common REST API Interview Questions
The most commonly asked REST API interview questions to prep for system design, software engineering, and other technical interviews.
Read more >
GitHub - Devinterview-io/api-design-interview-questions
Top 46 API Design interview questions (answered) for developers in 2021 · 1. What REST stands for? · 2. What are the core...
Read more >
API Design Interview Preparation - LinkedIn
Tai-Chia (Marcus) Huang · What Are the Goals for API Design for this System? · Define Unit Resources in the System · How...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found