-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Changing state re-renders the component in a way that breaks animation #51
Comments
So you want to be able to use (kind of) But it would be nice to be able to store particular examples in files. |
Now examples can have a state. Will it solve your issue? |
@chrisdrackett any feedback? I'd like to close this ticket. |
I'll work on this today so you guys can close the ticket. Thanks for checking in, and sorry for the delay! |
ok, so I hooked this up on one of my components today. this is a switch component that animates between two states. For some reason in the styleguide the component jumps between its two states without animation. Its almost like its being re-rendered from scratch on state change? |
That’s how it works now. |
@sapegin, could you explain this further? I see that the example styleguide modal uses Ah, I got it, it's done in the
...and then rendering this one in the nested |
I should try to do it, or you can try if you have time ;-) |
P.S. I realized one more thing: indepentently of what I've written above, the way you pass |
Seems like related. @sapegin, so how about using |
Yes, I'll have a look at that. |
I am stuck with this issue as well. It's pretty annoying to have a state that break all the rendering :/ |
I'm using my own wrapper to store state: /* tslint:disable:no-any */
import * as React from 'react';
import Debug from '../debug/debug';
export interface IPlaygroundProps extends React.Props<Playground> {
initialState?: IPlaygroundState;
showState?: boolean;
visibleEvents?: number;
children: (
state: IPlaygroundState,
setState: (state: IPlaygroundState) => void,
log: (type: string, event: any) => void
) => JSX.Element;
}
export interface IEvent {
date: Date;
value: any;
type: string;
}
export interface IPublicState {
[key: string]: any;
}
export interface IPlaygroundState {
events?: IEvent[];
publicState?: IPublicState;
}
export default class Playground extends React.Component<IPlaygroundProps, IPlaygroundState> {
public static defaultProps = {
visibleEvents: 3,
};
public constructor(props: IPlaygroundProps) {
super(props);
this.state = {
events: [],
publicState: props.initialState || {},
};
}
public render() {
return (
<div>
{this.props.children(
this.state.publicState,
(state) => this.handleStateChange(state),
(type, event) => this.handleLogEvent(type, event)
)}
{this.props.showState &&
<div style={{marginTop: 8}}>
<Debug force value={this.state.publicState} />
</div>
}
{this.state.events.slice(0, this.props.visibleEvents).map((event) => (
<div style={{marginTop: 8}}>
<Debug
force
title={`Event '${event.type}', ${event.date.toUTCString()}`}
value={event.value}
/>
</div>
))}
</div>
);
}
private handleLogEvent(type: string, value: any) {
const events = [
{
date: new Date(),
value,
type,
},
].concat(this.state.events);
this.setState({
events,
});
}
private handleStateChange(state: IPublicState) {
this.setState({
publicState: Object.assign({}, this.state.publicState, state),
});
}
}
(window as any).Playground = Playground; Usage: Default:
<window.Playground initialState={{exampleValue: -0.5}} showState>
{(state, setState) => (
<NumberField
value={state.exampleValue}
onValueChange={value => setState({exampleValue: value})}
/>
)}
</window.Playground> |
@vslinko Thanks for sharing this. Here is my simpler version (flow) // @flow
import { Element, Component } from "react"
type Props = {
initialState?: State,
children: (
state: State,
setState: (state: State) => void,
) => Element,
}
type State = {
[key: string]: any,
}
export default class Playground extends Component<void, Props, State> {
state: State;
constructor(props: Props) {
super(props)
this.state = (typeof props.initialState === "object")
? props.initialState
: {}
}
handleStateChange(state: State) {
this.setState(state)
}
render(): Element {
return (
this.props.children(
this.state,
(state) => this.handleStateChange(state),
)
)
}
}
(window).Playground = Playground |
Sorry didn't understand what to do :/ |
Fixed in #134. |
First off I want to say I'm pretty excited about this project. I've been working on my own styleguide based on much of the same tech, so I'm excited to potentially work along side others.
One thing I'm currently doing is letting example files be either markdown or full on react components. This way if I need to store state or similar to show examples of how a component works I can. I'm still digging into your code, so maybe this is currently possible, but if not it might be a nice thing to have if there is a simple way to provide it.
The text was updated successfully, but these errors were encountered: