Skip to content
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

blog-2 #7

Open
wants to merge 1 commit into
base: blog-1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/hello-world/src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {createRoot} from 'react-dom'


const comp = <div>hello world</div>
console.log(comp)
console.log(createRoot(document.getElementById("root")))
Expand Down
4 changes: 3 additions & 1 deletion packages/react/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ shared = { path = "../shared" }
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying.
console_error_panic_hook = { version = "0.1.7", optional = true }
web-sys = { version = "0.3.69", features = ["console"] }
web-sys = { version = "0.3.69", features = ["console"] }
js-sys = "0.3.69"
log = "0.4.21"

[dev-dependencies]
wasm-bindgen-test = "0.3.34"
Expand Down
38 changes: 34 additions & 4 deletions packages/react/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,36 @@
use js_sys::{Object, Reflect};
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub fn jsxDEV() -> String {
"hello world".to_string()
}
use shared::REACT_ELEMENT_TYPE;

#[wasm_bindgen(js_name = jsxDEV)]
pub fn jsx_dev(_type: &JsValue, config: &JsValue, key: &JsValue) -> JsValue {
let react_element = Object::new();
Reflect::set(
&react_element,
&"&&typeof".into(),
&JsValue::from_str(REACT_ELEMENT_TYPE),
)
.expect("$$typeof panic");
Reflect::set(&react_element, &"type".into(), _type).expect("_type panic");
Reflect::set(&react_element, &"key".into(), key).expect("key panic");

let conf = config.dyn_ref::<Object>().unwrap();
let props = Object::new();
for prop in Object::keys(conf) {
let val = Reflect::get(conf, &prop);
match prop.as_string() {
None => {}
Some(k) => {
if k == "ref" && val.is_ok() {
Reflect::set(&react_element, &"ref".into(), &val.unwrap()).expect("ref panic");
} else if val.is_ok() {
Reflect::set(&props, &JsValue::from(k), &val.unwrap()).expect("props panic");
}
}
}
}

Reflect::set(&react_element, &"props".into(), &props).expect("props panic");
react_element.into()
}
6 changes: 1 addition & 5 deletions packages/shared/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
pub struct REACT_ELEMENT_TYPE;

// pub enum ElementType {
//
// }
pub static REACT_ELEMENT_TYPE: &str = "react.element";