Last active
May 30, 2019 01:00
-
-
Save ryancharris/b0fb004feab040b187cdc5719d7507c0 to your computer and use it in GitHub Desktop.
LogRocket: React vs. Vue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <template> | |
| <div class="toggle"> | |
| <ToggleButton | |
| @clicked="workClicked" | |
| :class="{'toggle__button': true, 'active': workActive}" | |
| text="Work" | |
| /> | |
| <ToggleButton | |
| @clicked="homeClicked" | |
| :class="{'toggle__button': true, 'active': homeActive}" | |
| text="Home" | |
| /> | |
| </div> | |
| </template> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| React.useEffect(() => { | |
| axios({ | |
| method: "get", | |
| url: props.jsonUrl | |
| }) | |
| .then(res => { | |
| const { routes } = res.data; | |
| // setState is part of a useState hook unseen here | |
| setRoute(routes[0].legs[0].departure_time.text); | |
| }) | |
| .catch(err => { | |
| console.log(err); | |
| }); | |
| }, []); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| componentDidMount() { | |
| axios({ | |
| method: "get", | |
| url: this.props.jsonUrl | |
| }) | |
| .then(res => { | |
| const { routes } = res.data; | |
| this.setState({ | |
| departureTime: routes[0].legs[0].departure_time.text | |
| }); | |
| }) | |
| .catch(err => { | |
| console.log(err); | |
| }); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| created: function(jsonUrl) { | |
| axios({ | |
| method: "get", | |
| url: this.$props.jsonUrl | |
| }) | |
| .then(res => { | |
| const { routes } = res.data; | |
| this.departureTime = routes[0].legs[0].departure_time.text; | |
| }) | |
| .catch(err => { | |
| console.log(err); | |
| }); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| methods: { | |
| workClicked() { | |
| this.workActive = true; | |
| this.homeActive = false; | |
| this.$emit("toggled", "work"); | |
| }, | |
| homeClicked() { | |
| this.workActive = false; | |
| this.homeActive = true; | |
| this.$emit("toggled", "home"); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Child(props) { | |
| return ( | |
| <div> | |
| <button onClick={() => props.toggleView("work")}> | |
| Work | |
| </button> | |
| <button onClick={() => props.toggleView("home")}> | |
| Home | |
| </button> | |
| </div> | |
| ); | |
| } | |
| function Parent() { | |
| const [isWorkView, setWorkView] = React.useState(true); | |
| return <Child toggleView={setWorkView} />; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <div id="App"> | |
| <h1 className="App_header">My Cool App</h1> | |
| <Toggle onClick={handleToggleChange} /> | |
| {isWorkScreen ? <Work /> : <Home />} | |
| </div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <div id="app"> | |
| <h1 class="app__header">{{createHeader}}</h1> | |
| <Toggle @toggled="handleToggleChange"/> | |
| <Work v-if="isWorkScreen"/> | |
| <Home v-else/> | |
| </div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| methods: { | |
| handleToggleChange(newView) { | |
| this.isWorkScreen = Boolean(newView === "work"); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <Toggle @toggled="handleToggleChange"/> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export default { | |
| name: "App", | |
| components: { | |
| Home, | |
| Toggle, | |
| Work | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const { buses } = this.state; | |
| const busRoutes = buses.map(bus => { | |
| return ( | |
| <BusRoute | |
| key={`BusRoute-${bus.routeNumber}`} | |
| routeNumber={bus.routeNumber} | |
| origin={bus.origin} | |
| destination={bus.destination} | |
| jsonUrl={bus.jsonUrl} | |
| /> | |
| ) | |
| }) | |
| return ( | |
| <div className="Work"> | |
| {busRoutes} | |
| </div> | |
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <div class="Work"> | |
| <BusRoute | |
| v-for="bus in buses" | |
| :key="`BusRoute-${bus.routeNumber}`" | |
| :routeNumber="bus.routeNumber" | |
| :origin="bus.origin" | |
| :destination="bus.destination" | |
| :jsonUrl="bus.jsonUrl" | |
| /> | |
| </div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Accordion() { | |
| const [isAccordionOpen, toggleAccordion] = React.useState(false); | |
| return ( | |
| <div className="Accordion"> | |
| <h3>Accordion Header</h3> | |
| <button | |
| onClick={() => { | |
| toggleAccordion(!isAccordionOpen); | |
| }} | |
| > | |
| Toggle Accordion | |
| </button> | |
| {isAccordionOpen && <p>Accordion content lives here...</p>} | |
| </div> | |
| ); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <template> | |
| <div class="accordion"> | |
| <button @click="toggleAccordion">Toggle Accordion</button> | |
| <p v-if="isAccordionOpen">Accordion content lives here...</p> | |
| </div> | |
| </template> | |
| <script> | |
| export default { | |
| name: "Accordion", | |
| data: function() { | |
| return { | |
| isAccordionOpen: false | |
| }; | |
| }, | |
| methods: { | |
| toggleAccordion() { | |
| this.isAccordionOpen = !this.isAccordionOpen; | |
| } | |
| } | |
| }; | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment