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.

[Question] Readable atom with parameter or Another way

See original GitHub issue

Thank for great works.

I have some question, I develop custom calendar component.

Calendar component have selection map, for example:

// row are number of weekday, col are 7
const calendarSelectionMap: boolean[][] = [];

But, Calendar component don’t know user selected month so dynamic generation array on render time.

// phase 01, create component
const [state, dispatch] = useReducerAtom(mainAtom, reducer);
const rowCount: number = calculateCalendarRowCount(userSelectedMonth);
const days = calculateCalendarDay(userSelectedMonth);

useLayoutEffect(() => {
// phase02 initialize component state
dispatch({ type: 'create-selection-map', rowCount })
}, [ state, dispatch ]);

// phase03 render dom: only example
return (
    {_.times(rowCount).map((rowId) => {
      <div>
          {_.times(7).map((colId) => <div selected={{state.selection[rowId][colId]}}>{days[rowId][colId]}</div>
      </div>
    })}
);

but I got exception phase03, react SSR executed phase01 -> 03 -> 02.

I solve this problem that create wrapper function.

const onGetSelection = useCallback((rowId: number, colId: nubmer) => {
    try { 
       return state.selection[rowId][colId];
    } catch {
       return false;
   }
}, [state])

But I want use state value direct below,

<div selected={{state.selection[rowId][colId]}}>{days[rowId][colId]}</div>

So, I Think that

const readableAtom = atom(mainAtom, (get, param) => {
  const state = get(mainAtom); 

  try {
    return state.selection[param.rowId][param.colId];
  } catch {
    return false;
  } 
});

But I can’t found readable atom with any parameter. I miss understood mechanism?

Thank you for kindness

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
dai-shicommented, Sep 16, 2021

Maybe, I think your example getTodoItemAtom is add to primitives document it is great helpful content.

I wouldn’t at this point, as it’s not the primary recommendation. BUT, it might turn out to be something good with possibly other practice, so I will keep that in mind.

Let me leave another pattern with a custom hook, which may seem more idiomatic (not explicitly suggesting it, but just to expand our ideas):

const useGetTodoItemAtom = () => {
  const [todos] = useAtom(todosAtom)
  const getTodoItem = (id) => todos.filter((todo) => todo.id === id)
  return getTodoItem
)
1reaction
imjunicommented, Sep 15, 2021

@dai-shi

Thank you for your answer.

Great! that is perfect solution for my case. I don’t think that readable atom will be able to function.

Very helpful talk.

Maybe, I think your example getTodoItemAtom is add to primitives document it is great helpful content.

Thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

10 Essential Atom Editor Packages & Setup - YouTube
In this video I will start with a default Atom editor and add each of my favorite packages and plugins step by step...
Read more >
Problem to use prot_na.prm with cgenff.prm - CHARMM forums
Currently, whenever one attempts to combine one RTF/PARAM pair with another in CHARMM, it is necessary to make sure that both the atom...
Read more >
What is the use case of atomFamily in recoil? - Stack Overflow
When you call atomFamily() it will return a function which provides the RecoilState atom based on the parameters you pass in.
Read more >
258 questions with answers in CHARMM | Science topic
I would like to have the vdw force field parameters of different phosphate and ... I read that GROMACS has an option for...
Read more >
variable command - LAMMPS documentation
variable start timer other commands variable stop timer print "Elapsed ... Each time a set of per-atom values is read, a non-blank line...
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