Repository: livewire/livewire (php)

Issue:
skip render when redirect is set
**Is your feature request related to a problem? Please describe.**
I have a component that renders a model.  In this component is a delete button. When the delete is hit, the function removes the model and sets the redirect.  Control then passes to the render() function which tries to render the now deleted model, resulting in an error.

**Describe the solution you'd like**
Issue a redirect without needing to render the full component.

**Describe alternatives you've considered**
I have a workaround that checks `$this->redirectTo` in the render method, ane immediately returns.  Unfortunately it also needs to return a view, and this must be populated with valid data even though it will not be seen.
```
    public function render()
    {
        if($this->redirectTo){
            return view('livewire.foodbanks.foodbank-card')->withFoodbank(new Foodbank());
        }

        // etc with the required render functionality
```



Pull request:
Skip rendering on redirects
Fixes #940

Patch:
diff --git a/src/ComponentConcerns/PerformsRedirects.php b/src/ComponentConcerns/PerformsRedirects.php
index e1cdeb39..693c809f 100644
--- a/src/ComponentConcerns/PerformsRedirects.php
+++ b/src/ComponentConcerns/PerformsRedirects.php
@@ -9,15 +9,18 @@ trait PerformsRedirects
     public function redirect($url)
     {
         $this->redirectTo = $url;
+        $this->shouldSkipRender = true;
     }
 
     public function redirectRoute($name, $parameters = [], $absolute = true)
     {
         $this->redirectTo = route($name, $parameters, $absolute);
+        $this->shouldSkipRender = true;
     }
 
     public function redirectAction($name, $parameters = [], $absolute = true)
     {
         $this->redirectTo = action($name, $parameters, $absolute);
+        $this->shouldSkipRender = true;
     }
 }


Test patch:
diff --git a/tests/Unit/ComponentSkipRenderTest.php b/tests/Unit/ComponentSkipRenderTest.php
index b4c87ccd..08b4eecd 100644
--- a/tests/Unit/ComponentSkipRenderTest.php
+++ b/tests/Unit/ComponentSkipRenderTest.php
@@ -27,6 +27,15 @@ class ComponentSkipRenderTest extends TestCase
 
         $this->assertNull($component->payload['effects']['html']);
     }
+
+    /** @test */
+    public function on_redirect_in_mount_render_is_not_called()
+    {
+        $component = Livewire::test(ComponentSkipRenderOnRedirectInMountStub::class);
+
+        $this->assertEquals('/foo', $component->payload['effects']['redirect']);
+        $this->assertNull($component->payload['effects']['html']);
+    }
 }
 
 class ComponentSkipRenderStub extends Component
@@ -49,3 +58,16 @@ class ComponentSkipRenderStub extends Component
         return app('view')->make('null-view');
     }
 }
+
+class ComponentSkipRenderOnRedirectInMountStub extends Component
+{
+    public function mount()
+    {
+        $this->redirect('/foo');
+    }
+
+    public function render()
+    {
+        throw new \RuntimeException('Render should not be called on redirect');
+    }
+}