generated from seed-rs/seed-quickstart-webpack
-
Notifications
You must be signed in to change notification settings - Fork 41
Made template for React/Vue comparison + Seed 0.5.0 #2
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d4e15fd
Made template for React/Vue comparison
David-OConnor 9e6ad02
updated with new builder syntax
David-OConnor 25d7c0a
fix: compatible with Seed 0.5.0
MartinKavik cafaf8e
fix: seed version
MartinKavik 2f82ba7
fix: code_comparison upd
MartinKavik 58d2429
Merge pull request #5 from seed-rs/rosetta-stone-updates
David-OConnor 61cba52
Added ui example
David-OConnor d6cf17b
Removed WIP addition to fetch module
David-OConnor fd58fcb
Updated to use new import method
David-OConnor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| # Comparisons to React and Vue code | ||
|
|
||
| On this page, we'll show equivalent code snippets in Seed, and other frameworks. For now, we | ||
| include examples from `React` and `Vue`. | ||
| The [TodoMVC example](https://github.com/seed-rs/seed/tree/master/examples/todomvc) can be used to | ||
| compare to [its implementation in other frameworks](http://todomvc.com/). | ||
|
|
||
| Note that there are multiple ways to manage state in React, we've picked one where we store state | ||
| in the top-level component, and use functional components thereafter. | ||
| A closer structure match would be using it coupled with Redux. The Context API's an additional | ||
| way to handle it. We're also using Typescript. | ||
|
|
||
| ## A simple template, ready for state management | ||
|
|
||
| ## React | ||
|
|
||
| ```jsx | ||
| import * as React from "react" | ||
| import * as ReactDOM from "react-dom" | ||
|
|
||
| interface MainProps {} | ||
| interface MainState { | ||
| val: number | ||
| } | ||
|
|
||
| class Main extends React.Component<MainProps, MainState> { | ||
| constructor(props) { | ||
| super(props) | ||
|
|
||
| this.state = { | ||
| val: 0 | ||
| } | ||
|
|
||
| this.increment = this.increment.bind(this) | ||
| } | ||
|
|
||
| increment() { | ||
| this.setState({val: this.state.val + 1}) | ||
| } | ||
|
|
||
| render() { | ||
| return ( | ||
| <button onClick={() => this.state.increment()}> | ||
| {"Hello, World × " + this.state.val} | ||
| </button> | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| ReactDOM.render(<Main />, document.getElementById("app")) | ||
| ``` | ||
|
|
||
|
|
||
| ## Seed | ||
| From the Seed quickstart repo | ||
|
|
||
| ```rust | ||
| use seed::{*, prelude::*}; | ||
|
|
||
| struct Model { | ||
| pub val: i32, | ||
| } | ||
|
|
||
| impl Default for Model { // In this case, we could derive `Default` instead. | ||
| fn default() -> Self { | ||
| Self { | ||
| val: 0, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Clone)] | ||
| enum Msg { | ||
| Increment, | ||
| } | ||
|
|
||
| fn update(msg: Msg, model: &mut Model, _: &mut impl Orders<Msg>) { | ||
| match msg { | ||
| Msg::Increment => model.val += 1, | ||
| } | ||
| } | ||
|
|
||
| fn view(model: &Model) -> impl View<Msg> { | ||
| button![ | ||
| simple_ev(Ev::Click, Msg::Increment), | ||
| format!("Hello, World × {}", model.val) | ||
| ] | ||
| } | ||
|
|
||
| #[wasm_bindgen(start)] | ||
| pub fn render() { | ||
| App::builder(update, view) | ||
| .build_and_start(); | ||
| } | ||
| ``` | ||
|
|
||
| ## A component with attributes, styles, and events | ||
|
|
||
| ## React | ||
|
|
||
| ```jsx | ||
| const Form = ({name, color, value, changeText, doIt}: | ||
| {name: string, color: string, value: number, changeText: Function, doIt: Function}) { | ||
| // A description | ||
| const style: CSSProperties = {fontSize: 12, color: color} | ||
|
|
||
| return ( | ||
| <> | ||
| <input value={value.toString() onChange={(ev) => changeText(ev)} /> | ||
|
|
||
| <button | ||
| className="buttons" | ||
| title="Click me!" | ||
| style={style} | ||
| onClick={() => doIt()} | ||
| > | ||
| {name} | ||
| </button> | ||
| </> | ||
| ) | ||
| } | ||
| ``` | ||
|
|
||
| ## Seed | ||
|
|
||
| ```rust | ||
| /// A description | ||
| fn form(name: &str, color: &str, value: u32) -> Vec<Node<Msg>> { | ||
| let style = style!{St::fontSize => px(12), St::Color => color}; | ||
|
|
||
| vec![ | ||
| input![ attrs!{At::Value => value}, input_ev(Ev::Input, Msg::ChangeText)], | ||
|
|
||
| button![ | ||
| class!("buttons"), | ||
| attrs!{At::Title => "Click me!"}, | ||
| style, | ||
| simple_ev(Ev::Click, Msg::DoIt) | ||
| name, | ||
| ] | ||
| ] | ||
|
|
||
| } | ||
| ``` | ||
|
|
||
|
|
||
| ## Reusable UI items (todo) | ||
|
|
||
| ## HTTP Requests (todo) | ||
|
|
||
| ## Configuration files and tooling (todo) |
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
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
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
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Todo for next PRs
In all examples:
seed::Appreplace withAppand make sure we have the lineuse seed::{*, prelude::*}at the top of the example (if necessary) ; And we probably should addAppintoprelude.let app ... app.update(...is anti-pattern. It should be refactored withafter_mount.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated this PR and the Rust quickstart with this import style.