Skip to content

Instantly share code, notes, and snippets.

@chengsokdara
Last active November 11, 2019 16:31
Show Gist options
  • Save chengsokdara/8d877f88a319a92e1d1b91a81540ecae to your computer and use it in GitHub Desktop.
Save chengsokdara/8d877f88a319a92e1d1b91a81540ecae to your computer and use it in GitHub Desktop.
StoRa component sample
import React, { useEffect } from "react";
import useStora from "@rawewhat/stora";
const App = () => {
return (
<div>
StoRa Demo
<TestScreen />
<DemoScreen />
</div>
);
};
const TestScreen = () => {
const [states, actions] = useStora({
// add new action into testScreen actions
mutate: {
testScreen: {
newTestAction: () => {}
}
}
});
useEffect(() => {
// fire up action using dot access
actions.testScreen.testAction();
}, []);
console.log("states", states, "actions", actions);
return (
<button onClick={actions.testScreen.newTestAction}>Test Screen</button>
);
};
const DemoScreen = () => {
// get states and actions using destructuring syntax
const [
{
demoScreen: { newDemoState }
},
{
demoScreen: { demoAction }
}
] = useStora({
// add a new state into demoScreen states retaining previous states
mutate: {
demoScreen: {
newDemoState: "newDemoState"
}
},
// select only demoScreen states to return
query: "demoScreen"
});
console.log("states", states, "actions", actions);
return <button onClick={() => demoAction(true)}>Demo Screen</button>;
};
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment