Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add react instructions #418

Merged
merged 1 commit into from Sep 29, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 51 additions & 0 deletions docs/javascript/RHINO3DM.JS.md
Expand Up @@ -56,6 +56,57 @@ rhino3dm().then((rhino) => {
```


### React.js

Similar to plain html, **rhino3dm.js** can be added as a script in `index.js`

```js
// index.js
import { StrictMode } from "react";
import ReactDOM from "react-dom";

import App from "./App";

const rootElement = document.getElementById("root");

const script = document.createElement("script");
script.src = "https://cdn.jsdelivr.net/npm/rhino3dm@0.12.0/rhino3dm.min.js";
script.addEventListener("load", () => {
ReactDOM.render(
<StrictMode>
<App />
</StrictMode>,
rootElement
);
});
document.body.appendChild(script);

```

Once loaded, `rhino3dm` can be referenced in any component with `window.rhino3dm`, keep in mind that the wasm file loads asycnhronously so any any calls to wasm methods need to happen inside `.then()` or using `await`

```js
// App.js
import React, { useEffect, useState } from "react";
import "./styles.css";

export default function App() {
const [sphere, setSphere] = useState(null);

useEffect(() => {
window.rhino3dm().then((Module) => {
//creating a sphere using rhino3dm
setSphere(new Module.Sphere([1, 2, 3], 16));
});
}, []);

return <div className="App">{sphere && <p>{`sphere diameter is: ${sphere.diameter}`}</p>}</div>;
}
```

CodeSandbox of above react implementation can be found [here](https://codesandbox.io/s/rhino3dm-react-p3gr7?file=/src/App.js:0-428)


## API Docs

The latest [rhino3dm.js API Documentation](https://mcneel.github.io/rhino3dm/javascript/api/index.html)
Expand Down