# Synchronizing Perspective Component Props with State

**URL:** <https://forum.inductiveautomation.com/t/synchronizing-perspective-component-props-with-state/95277>\
**Category:** Module Development\
**Tags:** perspective\
**Created:** [August 28, 2024, 4:04pm UTC](https://forum.inductiveautomation.com/t/synchronizing-perspective-component-props-with-state/95277 "2024-08-28T16:04:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![kgamble](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kgamble/32/8019_2.png) [@kgamble](https://forum.inductiveautomation.com/u/kgamble)\
**Post date:** [August 28, 2024, 4:04pm UTC](https://forum.inductiveautomation.com/t/synchronizing-perspective-component-props-with-state/95277/1 "2024-08-28T16:04:54Z")

</div>

I am currently doing the following to keep my component props updated with the state of my component, however I feel like I am making this more complicated than it needs to be.. Is there a simpler way to update the prop `checked` in this example and keep it in sync with `state.isChecked` without having to write to it every time like this? Needing to call the `props.write` as well as the `this.setState` right beside each other is the part I am mostly curious about.

```python
export interface CheckboxProps {
    text?: string;
    checked?: boolean;
}

export class Checkbox extends Component<ComponentProps<CheckboxProps>, any> {
	constructor(props: ComponentProps<CheckboxProps>) {
		super(props);
		this.state = {
			isChecked: props.props.checked || false
		};
	}

    handleCheckboxChange = (event: React.ChangeEvent<HTMLInputElement>) => {
		this.props.store.props.write('checked', event.target.checked);
		this.setState({
			isChecked: event.target.checked
		});
    }

    render() {
		const { props: { text, checked }, emit } = this.props;

        return (
            <div {...emit()}>
                <label>
                    <input
                        type="checkbox"
                        checked={checked}
                        onChange={this.handleCheckboxChange}
                    />
                    {text}
                </label>
            </div>
        );
    }
}

```

Also, a separate question. Is it a best practice to use the props or the state when rendering the component? I could imagine that in some scenarios I want state elements that aren't props, in which I would need to use the state. However in this scenario I am curious if I have a state element that I have "synchronized" to a prop?

---

<div class="post-metadata">

**Author:** ![kgamble](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kgamble/32/8019_2.png) [@kgamble](https://forum.inductiveautomation.com/u/kgamble)\
**Post date:** [August 28, 2024, 4:15pm UTC](https://forum.inductiveautomation.com/t/synchronizing-perspective-component-props-with-state/95277/2 "2024-08-28T16:15:03Z")

</div>

For additional context, this is how I found that.

- [How to update custom component props in the designer - #3 by PGriffith](https://forum.inductiveautomation.com/t/how-to-update-custom-component-props-in-the-designer/85391/3)

Just trying to understand if thats the common strategy. Whenever you update the state to also call the `props.store.props.write` function

---

<div class="post-metadata">

**Author:** ![bmusson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmusson/32/87530_2.png) [@bmusson](https://forum.inductiveautomation.com/u/bmusson)\
**Post date:** [August 28, 2024, 5:31pm UTC](https://forum.inductiveautomation.com/t/synchronizing-perspective-component-props-with-state/95277/3 "2024-08-28T17:31:07Z")

</div>

My opinion; duplicate/redundant state is evil, and Perspective props should be preferred over internal state 99.99% of the time.

A Perspective component has different goals than a typical React component. The end user of a Perspective component is expecting to drag components into a view and configure them with bindings/change-scripts, both of which require the Perspective props to be the single source of truth.

If you have a complex use case where a Perspective component changes/modifies its state, cool, expose that as Perspective props so the end user can see and work with it.

---

<div class="post-metadata">

**Author:** ![kgamble](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kgamble/32/8019_2.png) [@kgamble](https://forum.inductiveautomation.com/u/kgamble)\
**Post date:** [August 28, 2024, 5:53pm UTC](https://forum.inductiveautomation.com/t/synchronizing-perspective-component-props-with-state/95277/4 "2024-08-28T17:53:59Z")

</div>

So to confirm, would this be the ideal structure for any props that a user may need to interact with?

```python
export interface CheckboxProps {
    text?: string;
    checked?: boolean;
}

export class Checkbox extends Component<ComponentProps<CheckboxProps>, any> {

    handleCheckboxChange = (event: React.ChangeEvent<HTMLInputElement>) => {
        this.props.store.props.write('checked', event.target.checked);
    }

    render() {
        const { props: { text, checked }, emit } = this.props;
        return (
            <div {...emit()}>
                <label>
                    <input
                        type="checkbox"
                        checked={checked}
                        onChange={this.handleCheckboxChange}
                    />
                    {text}
                </label>
            </div>
        );
    }
}

```

---

<div class="post-metadata">

**Author:** ![bmusson](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bmusson/32/87530_2.png) [@bmusson](https://forum.inductiveautomation.com/u/bmusson)\
**Post date:** [August 28, 2024, 6:03pm UTC](https://forum.inductiveautomation.com/t/synchronizing-perspective-component-props-with-state/95277/5 "2024-08-28T18:03:48Z")

</div>

Yup, that’s how I would do it.

And to test, I’d put two of the components on a view and bidirectionally bind their `checked` properties together. Clicking one checkbox should write to its props, trigger the bidirectional binding, which updates the props of the other checkbox triggering a re-render.
