Skip to content

feat: Improvement of SketchProps typing using Generics? #151

New issue

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

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

Already on GitHub? # to your account

Closed
junwen-k opened this issue Mar 16, 2022 · 3 comments · Fixed by #152
Closed

feat: Improvement of SketchProps typing using Generics? #151

junwen-k opened this issue Mar 16, 2022 · 3 comments · Fixed by #152
Assignees

Comments

@junwen-k
Copy link

First of all, thank you for this great project!
Right now custom props are not statically typed when using typescript. E.g,

<ReactP5Wrapper
    sketch={sketch}
    background="rgb(255, 255, 255)"  // this is not typed
/>
// props are not strictly typed
p5.updateWithProps = (props) => {
  ...
};

Maybe we can implement generics that allow users to specify custom props so the type will be correct. For instance,

const ReactP5WrapperComponent<P = SketchProps>: FC<P5WrapperProps<P>> = ({
  ...
});

Not tested, but it will probably look something like that.

@jamesrweb
Copy link
Collaborator

jamesrweb commented Mar 20, 2022

@junwen-k thats a great idea, I will look into this and try to get it into the next release when it happens.

@jamesrweb jamesrweb self-assigned this Mar 21, 2022
@jamesrweb jamesrweb linked a pull request Mar 21, 2022 that will close this issue
@Gresliebear
Copy link

Gresliebear commented Mar 21, 2022

I am not sure if this is related I want to lets say query data from a API and pass that to the sketch object.

App.js

import './App.css';
import { ReactP5Wrapper } from "react-p5-wrapper";
import sketch from './components/GraphSketch';


const dataObject = "hello"
// we watch to query data object and render onto network 


function App() {
  return (
    <div className="App">
      <div className='container'> 
      <ReactP5Wrapper sketch={sketch} dataObject={dataObject}/>
      </div>
    </div>
  );
}

GraphSketch.js


//   custom chart or chart.js
//   https://editor.p5js.org/aferriss/sketches/S1UTHZBHm

function sketch(p5, dataObject) {
  // we should pass size of div to createCanvas to reformat size of network graph to window?
  // 
    console.log("Hello from inside sketch", dataObject)
    p5.setup = () => p5.createCanvas(1280, 720, p5.WEBGL);
  
    p5.draw = () => {
      p5.background(250);
      p5.normalMaterial();
      p5.push();
      p5.rotateZ(p5.frameCount * 0.01);
      p5.rotateX(p5.frameCount * 0.01);
      p5.rotateY(p5.frameCount * 0.01);
      p5.plane(100);
      p5.pop();
    };
  }

export default sketch````


so on my console log I get Hello from inside sketch undefined

@jamesrweb
Copy link
Collaborator

@Gresliebear I think you should check the documentation on handling props as that shows you how to pass down props into your sketches.

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants