-
Notifications
You must be signed in to change notification settings - Fork 100
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
add Ref with react ref api #126
Comments
Check this out: https://codesandbox.io/s/4z26pmkp87 |
No, you should not use SFC. Using SFC, it's safe, but it's slow. export class Ref extends Component {
ref = React.createRef();
render() {
return this.props.children(this.ref);
}
} And besides, another feature request: export class Instance extends Component {
constructor(props) {
super(props);
props.init && props.init(this);
}
render() {
return this.props.children(this);
}
}
const list = [xxxx];
const to_render = <Instance init={ins => ins.audios = {}}>
{ins => <ul>
{list.map(item => <li key={item.id}>
<audio preload="none" src={item.url} ref={ele => ins.audios[item.id] = ele} />
<button onClick={e => ins.audios[item.id].play()}>play</button>
</li>)}
</ul>}
</Instance>
|
I'd say you should go with classes :) Component instance is bad abstraction because implementation detail is part of the api. |
@TrySound you are talking about Why I think Because |
I know it's hard, but mutable instance with methods as first class citizen is not the best api for immutable tree. Instance has a lot of stuff which is implementation detail in context of render prop. I would prefer to mutate state with values you need. |
But in some cases, we do really need mutable instance which won't cause a update when changing its state. Well, without |
Show me the case please |
Code in #126 (comment) showed the case. const list = [xxxx];
const to_render = <Instance init={ins => ins.audios = {}}>
{ins => <ul>
{list.map(item => <li key={item.id}>
<audio preload="none" src={item.url} ref={ele => ins.audios[item.id] = ele} />
<button onClick={e => ins.audios[item.id].play()}>play</button>
</li>)}
</ul>}
</Instance> |
It's okay to mutate state. const list = [xxxx];
const to_render = (
<State initial={{ audios: {} }}>
{({ state }) => (
<ul>
{list.map(item => (
<li key={item.id}>
<audio preload="none" src={item.url} ref={ele => state.audios[item.id] = ele} />
<button onClick={e => state.audios[item.id].play()}>play</button>
</li>
)}
</ul>}
</State>
) Or if you don't want to mutate state you may create mutable object on instance. Just don't pass instance with all its methods. Operate with data only here, mutable and immutable. class Mutable extends React.Component {
mutable = this.props.initial;
render() {
return this.props.children(this.mutable)
}
} |
Well, it's a debate between two API offering philosophy: In fact, I don't know which philosophy is better... -_-!!!! Component Choose which one you feel comfortable. :) |
The text was updated successfully, but these errors were encountered: