The props should not be directly updated as it breaks the rule of one way data flow. Consider creating local state in the component using the prop as initial state.
Aug 9, 2023
7
Level 8
Updating props
This is the concept that I really want to understand how it's done. I don't know what keyword to search for so I thought ask it here.
How do you update a prop or props after a method call? The prop is passed from the backend directly to the component so it doesn't have a parent component.
Example when using Inertia
// server side
public function index(Request $request)
{
// assuming that we're getting a count from a query.
$counts = [
'total_users' => 10,
'total_orders' => 25,
];
return inertia('index', $counts);
}
// client side: index component
<script setup>
const props = defineProps({
counts: {
type: Object,
default: null,
},
});
const submit = async () => {
// after a successful response, update the `counts` prop.
};
Or using Laravel Blade template + Vue
// server side
public function index(Request $request): View
{
// assuming that we're getting a count from a query.
$counts = [
'total_users' => 10,
'total_orders' => 25,
];
return view('index', [
'counts' => $counts,
]);
}
// blade template
<index-component :counts="{{ Js::from($counts) }}"></index-component>
// client side: index component
<script setup>
const props = defineProps({
counts: {
type: Object,
default: null,
},
});
const submit = async () => {
// after a successful response, update the `counts` prop.
};
Level 104
1 like
Please or to participate in this conversation.