Skip to content

Commit

Permalink
Merge pull request #32 from brianzinn/render-hook
Browse files Browse the repository at this point in the history
Render hook
  • Loading branch information
brianzinn committed Dec 1, 2019
2 parents fef9c18 + 8b8d037 commit 842b5bb
Show file tree
Hide file tree
Showing 10 changed files with 282 additions and 186 deletions.
57 changes: 0 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,63 +269,6 @@ const App: React.FC = () => {
}
```

## Using context

If you need to do something fancy with `scene`, `canvas`, or `engine`, there are a few ways:

### react hooks

```jsx
// use Hooks to get engine/canvas/scene
import { useBabylonEngine, useBabylonCanvas, useBabylonScene } from 'react-babylonjs'

// later inside a functional component:

export default () => {
const engine = useBabylonEngine()
const canvas = useBabylonCanvas()
const scene = useBabylonScene()
console.log({ engine, canvas, scene })

return (
<div>See console</div>
)
}

````

### HOC

```jsx
import { withBabylonJS, withScene } from 'react-babylonjs'
const DemoComponent = ({ scene, engine, canvas }} => {
console.log({ scene, engine, canvas })
return (
<div>See console</div>
)
}
export default withBabylonJS(withScene(DemoComponent))
```

### direct Consmuer

```jsx
import { WithSceneContext } from 'react-babylonjs'
const DemoComponent = ({ scene }} => {
const engine = scene.getEngine()
const canvas = engine.getCanvas()
console.log({ scene, engine, canvas })
return (
<div>See console</div>
)
}
export default () => (<WithSceneContext>{DemoComponent}</WithSceneContext>)
```

## Major Release History
> v1.0.0 (2018-11-29) - Add code generation, HoC, context provider
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@
"lodash.camelcase": "^4.3.0",
"prettier": "^1.15.3",
"prompt": "^1.0.0",
"react": "^16.8.6",
"react-dom": "^16.9.0",
"react-reconciler": "^0.21.0",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-reconciler": "^0.24.0",
"replace-in-file": "^3.4.3",
"rimraf": "^2.6.1",
"rollup": "^1.1.0",
Expand Down
6 changes: 2 additions & 4 deletions src/Engine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ export const BabylonJSContext = createContext<WithBabylonJSContext>({
canvas: null
})

export const BabylonJSContextConsumer = BabylonJSContext.Consumer

type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;

export function withBabylonJS<
Expand All @@ -34,8 +32,8 @@ export function withBabylonJS<
};
}

export const useBabylonEngine = () => useContext(BabylonJSContext).engine
export const useBabylonCanvas = () => useContext(BabylonJSContext).canvas
export const useBabylonEngine = (): Nullable<BabylonJSEngine> => useContext(BabylonJSContext).engine
export const useBabylonCanvas = (): Nullable<HTMLCanvasElement | WebGLRenderingContext> => useContext(BabylonJSContext).canvas

type EngineProps = {
babylonJSContext: WithBabylonJSContext,
Expand Down
2 changes: 1 addition & 1 deletion src/ReactBabylonJSHostConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ const ReactBabylonJSHostConfig: HostConfig<
},

canHydrateInstance: (instance: any, type: string, props: Props): null | CreatedInstance<any> => {
console.log("canHydrateInstance", instance, type, props)
// console.log("canHydrateInstance", instance, type, props)
return null
},

Expand Down
Loading

0 comments on commit 842b5bb

Please sign in to comment.