Skip to content

Instantly share code, notes, and snippets.

View becca-bailey's full-sized avatar

Becca Bailey becca-bailey

View GitHub Profile
@becca-bailey
becca-bailey / machine.js
Last active May 2, 2020 03:48
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
type AAndB = {
a: string;
b: string;
};
type Neither = {
someOtherProp: string;
};
type Props = AAndB | Neither;
var todos = []
function addTodo(task) {
todos.push({
task: task,
completed: false,
});
}
addTodo("Feed my cat");
it("can add an alert", () => {
const TestComponent = () => {
return (
<AlertProvider>
<Layout>
<ComponentWithAlert />
</Layout>
</AlertProvider>
);
};
class Layout extends React.Component {
render() {
return (
<AlertContext.Consumerr>
{({ alert }) => (
<React.Fragment>
{alert && <FancyAlertMessage alert={alert} />
{this.props.children}
</React.Fragment>
)}
const AlertContext = React.createContext<IAlertContext>({
alert: undefined,
showAlert: (alert) => {},
});
// Here we are creating a context with default values
class AlertProvider extends React.Component {
state = {
alert: undefined
};
<AlertProvider> // This context provides a way to display alerts and access alerts
<Layout> // This layout container can access the alert to display in the header
<Router>
<Route exact path="/" component={Home} /> // This route can create an alert to show in the layout
</Router>
</Layout>
</AlertProvider>
<AlertProvider> // This context provides a way to display alerts and access alerts
<Layout> // This layout container can access the alert to display in the header
<Router>
<Route exact path="/" component={Home} /> // This route can create an alert to show in the layout
</Router>
</Layout>
</Router>
class SimpleComponentWithAlert extends React.Component {
render() {
const myAlert = { message: "This is my alert", type: "error" };
return (
<AlertManager>
{({ alert, showAlert }) => (
<div>
{alert && <FancyAlertMessage alert={alert} />}
<button onClick={() => showAlert(myAlert)}>Show Alert</button>
</div>
class AlertManager extends React.Component {
state = {
alert: undefined
};
render() {
const { alert } = this.state;
const { children } = this.props;
return children({ alert, showAlert: this.showAlert });
}