Skip to content

Instantly share code, notes, and snippets.

@Youngestdev
Created February 15, 2019 04:23
Show Gist options
  • Select an option

  • Save Youngestdev/5f41ab6afc0d4f7f3a6898e671293594 to your computer and use it in GitHub Desktop.

Select an option

Save Youngestdev/5f41ab6afc0d4f7f3a6898e671293594 to your computer and use it in GitHub Desktop.
Actually, messing around unethical ideas and sketching rough codes.
import React, { useEffect, useContext, useRef } from 'react';
export const NodeMounter = props => {
const ref = useRef('');
const { node } = props;
ref.appendChild(node);
useEffect(( {node: prevNode} ) => {
const { node } = props;
ref.current.removeChild(prevNode);
ref.current.appendChild(node);
return () => {
const { node } = props;
ref.current.removeChild(node);
}
});
return <g ref={ref} />;
};
export const RoughContext = React.createContext();
@Youngestdev
Copy link
Copy Markdown
Author

Okay, basically rewrote the NodeMounter from class to function component. Not much difference per say and actually wasn't necessary. I was just hooking myself lol.

Rewriting that component made 20 test pass but the svg test didn't pass due to the lifecycle methods, since I actually haven't dug into the hooks deeply, Idk why that test fails. Is there a specific way to write tests for hooks? - perhaps, Kent said something bout it actually. Nontheless, as a pro and expert, you might have an explanation before I begin burning my data πŸ˜€

@Youngestdev
Copy link
Copy Markdown
Author

@ooade
Copy link
Copy Markdown

ooade commented Feb 15, 2019

That will actually make the NodeMounter rerender as much time as the SVG changes. No check for changes in node. You'd have to use react-testing-library to test for hooks, and we currently don't have that. There's a utility there called testHooks that'll help with that.

One more thing, if NodeMounter changes to a functional component, then ReactRough component will have to be changed as well, so we'll be fully on hooks.

@Youngestdev
Copy link
Copy Markdown
Author

Hmm. Interesting, any plan on moving to hooks soon ?

@ooade
Copy link
Copy Markdown

ooade commented Feb 15, 2019

Yeah, I'm kinda busy at the moment tho πŸ˜…
Working on an IVR.

@Youngestdev
Copy link
Copy Markdown
Author

Mind if I rewrite to use hook since it's an open source project ?. It'd be a nice way to learn React more πŸ˜„

@Youngestdev
Copy link
Copy Markdown
Author

Still learning how to write hooks. Sadly, I wrote this ( pardon me if it's bad code, I fore apologise ): Anyways, here is the ReactRough component. But, test doesn't pass 😒 . The reason the test doesn't pass is because there's smtn I'm not doing right in the Context Consumer-Provider API.

const ReactRough = (props, rc, ctx, rendererRef)  => {

    const { renderer, width, height, backgroundColor } = props;

    const forceUpdate = useForceUpdate();

    rendererRef = useRef();

    useEffect(() => {
        const { renderer } = props;
        ctx =
            renderer === 'canvas' && rendererRef.current.getContext('2d');
        rc = Rough[renderer](rendererRef.current);
        // TODO: this.forceUpdate();
        forceUpdate();
    }, []);


    const clearCanvas = () => {
        const { backgroundColor, width, height } = props;
        // If this is the first render the ctx will be null
        // It will be cleared later in the useEffect arc
        if ( !ctx ) {
            return;
        }

        if ( backgroundColor ) {
            ctx.save();
            ctx.fillstyle = backgroundColor;
            ctx.fillRect(0, 0, width, height);
            ctx.restore();
        } else {
            ctx.clearRect(0, 0, width, height);
        }
    };

    const redraw = () => forceUpdate();

    // let children = props.children;


    const rendererOptions = {
        width,
        height
    };

//    First clear the canvas in case of a new render.
    if ( renderer === 'canvas') {
        clearCanvas();
    } else {
        rendererOptions.style = { backgroundColor };
    }

    const Renderer = renderer;

    return (
        <RoughContext.Provider value={{ rc: rc, renderer}}>
            <Renderer {...rendererOptions} ref={rendererRef}>
                {props.children}
            </Renderer>
        </RoughContext.Provider>
    );

};

@Youngestdev
Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment