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

Pass function as property? #12

Closed
sjorsvanheuveln opened this issue Sep 30, 2017 · 4 comments
Closed

Pass function as property? #12

sjorsvanheuveln opened this issue Sep 30, 2017 · 4 comments

Comments

@sjorsvanheuveln
Copy link

Can I also pass a function as a property let's say something like this:

<P5Wrapper sketch={sketch} action={actionFunction} />

//sketch
export function sketch(P) {
  const p = P;

  p.setup = function (props) {
    p.createCanvas(width, height);
   props.actionFunction();
  };

  p.draw = function () {
    }
  };
}

I've tried and failed. Is it possible to do this?

@karismatic-megafauna
Copy link

karismatic-megafauna commented Oct 25, 2017

Two things here:

  1. in your code, this line => props.actionFunction();
    should actually be => props.action();
    this is because the prop key you are assigning actionFunction to is action.

  2. even if that isn't the issue, i don't think you can access props in this way with this wrapper... (where does props come from?)

@karismatic-megafauna
Copy link

karismatic-megafauna commented Oct 25, 2017

Oh, i think the answer is here => https://github.com/NeroCor/react-p5-wrapper#usage

Where it is defined:

  p.myCustomRedrawAccordingToNewPropsHandler = function (props) {
    if (props.rotation){
      rotation = props.rotation * Math.PI / 180;
    }
  };

and where it is used:

  <P5Wrapper sketch={sketch} rotation={rotation} />

notice that the name "myCustomRedrawAccordingToNewPropsHandler" is important:

in the source code, you need to call that name exactly to get rotation on your props.

@jamesrweb
Copy link
Collaborator

@karismatic-megafauna, @sjorsvanheuveln yeah, this is how it works from what I have seen too, I refactored some code in #48 but hoping to also edit this because it is actually implemented in such a way that it polluted the p5 namespace as you can see at index.ts line 30.

Currently I think I will propose a simple options object to be kept in state and passing down the setOptions handler for people to Lift state up and read-on-render as required. Any thoughts on this?

@jamesrweb jamesrweb linked a pull request Nov 25, 2020 that will close this issue
@jamesrweb
Copy link
Collaborator

Closing due to inactivity. For those viewing this issue in the future, a related PR of interest could be #65 depending how that one develops.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants