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.

Simple counter example as a component class

See original GitHub issue

So I was trying to demo the concept of Sprig class components to a colleague with a simple example

<?php

namespace sprig\components;

use putyourlightson\sprig\base\Component;

class HelpSupport extends Component
{
    public int $count = 2;
    protected $_template = '_components/helpSupport';

    public function updateCount()
    {
        $this->count = $this->count * 2;
    }
}

and then the component

<div class="container"
     id="helpSupport">
    <form sprig
          s-action="updateCount"
          s-replace="#hello"
    >
        <button>
            increase counter
        </button>
    </form>
</div>

<div id="hello">
    {{ count }}
</div>

it works on the first click but then stops. What’s missing?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

6reactions
bencrokercommented, Sep 1, 2021

That s-replace crept back in somehow, removing it should work.

<div class="container"
     id="helpSupport">
        <button sprig
          s-action="updateCount"
          s-val:count="{{ count }}">
            increase counter
        </button>
</div>

<div id="hello">
    {{ count }}
</div>
0reactions
bencrokercommented, Sep 2, 2021

Since we’re already doing this, here would be my version of a counter component.

<button sprig s-action="decrement">-</button>
<input type="text" value="{{ count }}">
<button sprig s-action="increment">+</button>
Read more comments on GitHub >

github_iconTop Results From Across the Web

Simple Counter App in React JS - with Functional and Class ...
We'll make a Simple Counter App in React JS - with Functional and Class Based Component. In this counter application, we've shown two...
Read more >
A simple counter using React - CodeSandbox
This is a simple counter that displays the things that you can do with React component. 7. 257.6k.
Read more >
How to Build a Simple Counter App in React - Techomoro
This is a simple guide to creating a simple counter app using React. It will help you to get an idea about React...
Read more >
React Counter Button - Will Vincent
Let's build a simple counter button in React that will update every time it is clicked. To start, create a new React app...
Read more >
React - Class Component for Counter - CodePen
1. class Counter extends React.Component { ; 2. constructor(props) { ; 3. super(props); ; 4. this.state = { ; 5. count: 0.
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