r
r
react-juce
Search…
JS/Native Interop
There are generally two ways to cross the Native/JavaScript boundary:
    1.
    You can pass C++ values and functions into JavaScript, and JavaScript values and functions into C++ directly or via an event model
    2.
    Or, you can create a custom native View and render it in your React component tree

Value Passing

To pass C++ values and functions into your JS environment, see EcmascriptEngine::registerNativeProperty and EcmascriptEngine::registerNativeMethod. These methods will install the given values on either the global object, or named object of your choosing.
In the other direction, you can pass values from JavaScript into C++ either via invoking the methods that were installed via EcmascriptEngine::registerNativeMethod or by assigning a value to a named object accessible from the global object, and looking that value up or invoking it on the C++ side with EcmascriptEngine::evaluateInline or EcmascriptEngine::invoke.

Example

1
// From C++
2
engine->registerNativeProperty("world", "Hello!");
3
engine->registerNativeMethod("sayHello", [this](juce::var::NativeFunctionArgs const& args) {
4
// Here, `args.arguments[0]` comes from JavaScript
5
std::cout << args.arguments[0].toStdString() << std::endl;
6
});
Copied!
1
// Then, from js
2
console.log("Hello", global.world);
3
global.sayHello("World!");
Copied!
And similarly, in the other direction:
1
// From js
2
global.myFun = (x) => x * x;
3
global.myValue = 17;
Copied!
1
// From C++
2
auto myValue = engine->evaluateInline("global.myValue");
3
auto result = engine->invoke("myFun", (int) myValue);
Copied!

Custom Native View

Now the other option is to define a totally custom native view, and register it with React. To do this, you can make your own C++ class that inherits reactjuce::View, and override the necessary methods. Basically, a reactjuce::View is just a juce::Component with some extra goodies.
For greater detail, see Custom Native Components, but for a brief example, see below.

Example

1
class MyCoolView : public reactjuce::View {
2
void paint(juce::Graphics& g) override {
3
// Do your own paint routine like usual.
4
// You can also treat this whole class instance like your normal juce::Components. Add children, `addAndMakeVisible`,
5
// `resized` and everything!
6
}
7
}
Copied!
Now once you've got your custom view implementation, you have to tell React about it so that you can use it. The end goal with this is to be able to write something like this:
1
function MyReactApp(props) {
2
return (
3
<View>
4
<MyCoolView customProp="hi" otherCustomProp="bye" />
5
</View>
6
);
7
}
Copied!
With this, you can write your juce::Components like normal, but use React to compose these things together into your full application. So, how do we register your C++ class with React? All you need is ReactApplicationRoot::registerViewType:
1
mAppRoot.registerViewType("MyCoolView", []() {
2
// This lambda will be called when we need to construct one of your custom view instances. So, first, we make one:
3
auto view = std::make_unique<MyCoolView>();
4
5
// Then we need to construct a shadow view, which will govern the layout. 99% of the time, you can just use the
6
// default shadow view:
7
auto shadowView = std::make_unique<ShadowViewType>(view.get());
8
9
// Then just return these guys as a pair
10
return {std::move(view), std::move(shadowView)};
11
});
Copied!
That's it, React can now understand your MyCoolView!
Now there's one last cosmetic issue. In React, you can always make elements with React.createElement("MyCoolView", props, children);, but you rarely see that because people are used to using JSX and writing <MyCoolView {...props}>. If you want to use JSX like that you have to make this little wrapper to abstract over the dynamic string argument:
1
function MyCoolView(props) {
2
return React.createElement("MyCoolView", props, props.children);
3
}
4
5
// Now I can use it elsewhere in JSX...
6
function MyApp(props) {
7
return <MyCoolView {...props} />;
8
}
Copied!
Last modified 8mo ago