0

I have a save button with an image (bordered icon) . I want to change icon color on button click is success

const saveClickProperty = () => {
    console.log("test");
    if(userId) {
        setLoading(true);
        propertyUserSave({
            userId: userId,
            propertyId: property._id
        }).then((resp) => {
            console.log("hello");
            openNotification("", "Property Saved");
            if (isSuccess(resp)) {
                console.log("inside");
                openNotification(userMessage.successHeader, userMessage.propertySaved);  
            }
        }).finally(() => setLoading(false));
    }
}

and i want to change save.png

<div className="saveAction">
                {userId && 
                    <a onClick={saveClickProperty} className="">
                        <div className="ActionDiv">
                            <img src="/images/index/save.png" alt="save" /> {' '} Save
                        </div>
                    </a>
                }

            </div>

right now this is look like enter image description here

Any idea to change save.png image color after button click (i mean when property is successfully saved).

2
  • Can you include the CSS used, i.e. for classes saveAction and ActionDiv, anything that is styling the button. I don't think you can just "change" the color of a png file, but you may be able to do some filter/overlay. Can you also provide clearer expected result? Is it simply after the button is clicked change the color, or after some success state value is updated? Commented Jun 17, 2020 at 7:54
  • @DrewReese i want to do like similarly facebook like button is only bordered and after click it filled with color. I want to make my button same , when some one click to save , after success message color of heart on button must be change with some color. Commented Jun 17, 2020 at 7:57

1 Answer 1

2

A possible solution is to create a state and after the property is saved, change that state, in order to the components (icon colors and image) re-render.

Example for State:

const [success, setSucess] = useState(false);

Example for Image:

const sucessImage = <img src="/images/index/NEWIMAGE.png" alt="save" />;
const defaultImage = <img src="/images/index/save.png" alt="save" />;

    <div className="saveAction">
                    {userId && 
                        <a onClick={saveClickProperty} className="">
                            <div className="ActionDiv">
                                ${success ? sucessImage : defaultImage} {' '} Save
                            </div>
                        </a>
                    }

                </div>

Example of changing the state:

...
        }).then((resp) => {
            setSucess(true);
            console.log("hello");
            openNotification("", "Property Saved");
Sign up to request clarification or add additional context in comments.

1 Comment

Exactly i was looking for this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.