Single atom for updating a large list
See original GitHub issueIs that the best practice?
I’ve seen example like Todolist on the website, it writes
const todoListState = atom({
key: 'todoListState',
default: [],
});
function TodoItem({item}) {
const [todoList, setTodoList] = useRecoilState(todoListState);
const index = todoList.findIndex((listItem) => listItem === item);
const editItemText = ({target: {value}}) => {
const newList = replaceItemAtIndex(todoList, index, {
...item,
text: value,
});
setTodoList(newList);
};
const toggleItemCompletion = () => {
const newList = replaceItemAtIndex(todoList, index, {
...item,
isComplete: !item.isComplete,
});
setTodoList(newList);
};
const deleteItem = () => {
const newList = removeItemAtIndex(todoList, index);
setTodoList(newList);
};
return (
<div>
<input type="text" value={item.text} onChange={editItemText} />
<input
type="checkbox"
checked={item.isComplete}
onChange={toggleItemCompletion}
/>
<button onClick={deleteItem}>X</button>
</div>
);
}
function replaceItemAtIndex(arr, index, newValue) {
return [...arr.slice(0, index), newValue, ...arr.slice(index + 1)];
}
function removeItemAtIndex(arr, index) {
return [...arr.slice(0, index), ...arr.slice(index + 1)];
}
function TodoList() {
const todoList = useRecoilValue(todoListState);
return (
<>
{/* <TodoListStats /> */}
{/* <TodoListFilters /> */}
<TodoItemCreator />
{todoList.map((todoItem) => (
<TodoItem key={todoItem.id} item={todoItem} />
))}
</>
);
}
This makes sense to me, however how is the performance of it if I actually need to create a whole new list every time one element is changed within the todo list. I am having a similar application where I have to update the cells of a spreadsheet, which does not seem so optimal if I were taking this approach.
Is there a better way for solving this problem or how can we improve upon that?
I have something in my mind that is using atomFamily
and I wrote it as
type CellValueQuery = {
workspaceId: string,
tableId: string,
recordId: string,
fieldId: string
}
export const cellValueStates = atomFamily<CellValue, CellValueQuery>({
key: 'cell-value-states',
default: null,
});
function Cell({cellValueQuery}) {
const [cellValue, setCellValue] = useRecoilState(cellValueStates(cellValueQuery));
const editItemText = ({target: {value}}) => {
setCellValue(value);
};
return (
<div>
<input type="text" value={item.text} onChange={editItemText} />
<input
type="checkbox"
checked={item.isComplete}
onChange={toggleItemCompletion}
/>
<button onClick={deleteItem}>X</button>
</div>
);
}
Am I doing the right thing here?
Issue Analytics
- State:
- Created 3 years ago
- Reactions:3
- Comments:12 (1 by maintainers)
Top Results From Across the Web
neigh_modify command
The indices of neighboring atoms are stored in “pages”, which are allocated one after another as they fill up. The size of each...
Read more >Nano-sized islands open possibilities for application of single ...
A new method to anchor single atoms of platinum-group metals on nanometer-sized islands allows for efficiently using these expensive metals ...
Read more >Updating Parts of Documents
The first is atomic updates. This approach allows changing only one or more fields of a document without having to re-index the entire...
Read more >Upgrading Your Package
Run Atom in Dev Mode, atom --dev , with your package loaded, and open Deprecation Cop (search for "deprecation" in the command palette)....
Read more >How to Update Members of a Collection with LINQ
Getting to One Line of Code If you're willing to use the ToList method to convert the collection into a List, you can...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I struggled a bit with this issue too. I ended up splitting the atoms this way:
The atoms are used like this:
The reason why
todoState
uses selectorFamily is that I wanted to fail hard in cases where invalid primary key was supplied. I guess todoState could also as follows:Entire project can be found here: https://github.com/harjis/react-recoil
Let me know if there is a better way to achieve similar behavior.
yeah, the feature is useful. but it is not enabled yet. see: #845
I build a local bundle of recoil, enable the feature with
gkx.setPass('recoil_suppress_rerender_in_callback');
then the selector return the immutable value such as:
code: https://codesandbox.io/s/objective-matsumoto-7qilr?file=/src/App.js online demo:https://7qilr.csb.app/
it works.
I am looking forward to this feature, and thanks to @drarmstr