Skip to content

How get react global value from c#? #103

Closed
@alec1o

Description

@alec1o

Discussed in #102

Originally posted by alec1o December 3, 2023
Im try get react value from c# but i don't know how it really works, because docs don't explain very well.

using ReactUnity.UIToolkit;
using UnityEngine;

public class TestReactUnity : MonoBehaviour
{
    public ReactRendererUIToolkit react;
    public uint delay = 3;
    private uint _count = 0;
    private float _timer;

    private void Start()
    {
        react.Globals.AddListener((e) =>
        {
            string listener = $"len({e.Count})";

            foreach (var i in e)
            {
                dynamic r = i.Value;
                listener += $"\n{i.Key} -> {r}";
            }

            print($"listener event: {listener}");
            print(react.Globals["reactside"]);
        });
    }

    private void Update()
    {
        _timer += Time.deltaTime;

        if (_timer > Mathf.Clamp(delay, 1, 100))
        {
            _timer = 0;
            _count++;
            // add only.
            // react.Globals.Add("csharpside", $"C# counter {_count}");

            // add or update
            react.Globals.Set("csharpside", $"C# counter {_count}");


        }
    }
}
const globals = useGlobals()

    return (<>
        <h1 style={{ color: 'white' }}>Ola Mundo, C#: {globals.csharpside}</h1>
        <h1 style={{ color: 'white' }}>Ola Mundo, JS: {globals.reactside}</h1>

        <button onClick={() => { Globals.reactside++; }}>
            Increase ReactSide
        </button>
    </>)
}

How can i access reactside value from c#?

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions