By Guillaume Briday
import { useState } from 'react'
function MyComponent() {
const [name, setName] = useState('')
const [items, setItems] = useState([])
useEffect(() => {
fetch(`/my/awesome/api?name=${name}`)
.then(res => res.json())
.then((data) => {
setItems(data)
})
}, [name])
return (
...
{items.map(item => (
-
{item.name}
))}
)
}
From manipulating DOM with jQuery to manipulate Data with React.
Maybe you just need Turbo and Stimulus...
And www.stimulus-components.com π
<%# app/views/todos/index.html.erb %>
Todos
<%= turbo_frame_tag 'todos_list' do %>
<%= link_to 'All', todos_path %>
<%= link_to 'Completed', todos_path(completed: true) %>
<%= link_to 'In progress', todos_path(completed: false) %>
<% end %>
See: Modern Datatables
class CommentsController < ApplicationController
def index
budget = Budget.find(params[:budget_id])
render partial: 'comments/comments',
locals: { comments: budget.comments }
end
end
<%= turbo_frame_tag :comments,
src: comments_path(budget_id: @budget.id),
loading: 'lazy' do %>
<%# Adding a nice loader that will be replaced once the partial gets loaded. %>
<% end %>
<%= turbo_refreshes_with method: :morph, scroll: :preserve %>