Skip to content

Commit dac7372

Browse files
jopekstaltz
authored andcommitted
update docs with react generated event handlers based on reactSource.select(sel).events('whatever')
1 parent 754fa24 commit dac7372

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

readme.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,57 @@ The `sel` can be a string or a symbol. We recommend using symbols to avoid strin
143143
</p>
144144
</details>
145145

146+
<details>
147+
<summary><strong>Pass event handlers as props to react components</strong> (click here)</summary>
148+
<p>
149+
150+
Use hyperscript `h` and pass a **`sel`** as a prop. Use that selector in `sources.react.select(sel).events(whatever)` to have cyclejs/react pass an `onWhatever` function to the react component:
151+
152+
```js
153+
import React from "react";
154+
import ReactDOM from "react-dom";
155+
import "./index.css";
156+
157+
import { makeComponent, h } from "@cycle/react";
158+
159+
// react coponent
160+
function Welcome(props) {
161+
return (
162+
<div>
163+
<h1>Hello, {props.name}</h1>
164+
<button onClick={() => props.onPressWelcomeButton({ random: Math.random().toFixed(2) }) } >
165+
press me
166+
</button>
167+
</div>
168+
);
169+
}
170+
171+
// cyclejs component
172+
function main(sources) {
173+
const click$ = sources.react
174+
.select("welcomeSel")
175+
.events("pressWelcomeButton")
176+
.debug('btn')
177+
.startWith(null);
178+
179+
const vdom$ = click$.map(click =>
180+
h("div", [
181+
h(Welcome, { sel: "welcomeSel", name: "madame" }),
182+
h("h3", [`button click event stream: ${click}`])
183+
])
184+
);
185+
186+
return {
187+
react: vdom$
188+
};
189+
}
190+
191+
const Component = makeComponent(main);
192+
ReactDOM.render(<Component />, document.getElementById("root"));
193+
```
194+
</p>
195+
</details>
196+
146197
<details>
147198
<summary><strong>Isolate event selection in a scope</strong> (click here)</summary>
148199
<p>

0 commit comments

Comments
 (0)