Skip to content

Commit

Permalink
Upgrade to React 18 and useSyncExternalStore (atlassian#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
albertogasparin committed Aug 11, 2022
1 parent 9ce56ee commit dba9676
Show file tree
Hide file tree
Showing 27 changed files with 18,173 additions and 381 deletions.
1 change: 1 addition & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module.exports = {
},
],
'@babel/preset-react',
'@babel/preset-typescript',
'@babel/preset-flow',
],
env: {
Expand Down
1 change: 0 additions & 1 deletion docs/advanced/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
- [Selectors](./selector.md)
- [Containers](./container.md)
- [Devtools](./devtools.md)
- [Performance tips](./performance.md)
- [State rehydration](./rehydration.md)
- [Middlewares](./middlewares.md)
- [Custom mutator](./mutator.md)
35 changes: 0 additions & 35 deletions docs/advanced/performance.md

This file was deleted.

13 changes: 4 additions & 9 deletions examples/advanced-flow/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow
import React, { useState, StrictMode } from 'react';
import ReactDOM from 'react-dom';
import ReactDOM from 'react-dom/client';
import { defaults, type Middleware } from 'react-sweet-state';

import { UserListRpc, UserListHook } from './views/user-list';
Expand All @@ -23,11 +23,6 @@ defaults.middlewares.add(mw);
*/
defaults.devtools = true;

/**
* Enable Batch updates
*/
defaults.batchUpdates = true;

/**
* Main App
*/
Expand Down Expand Up @@ -57,9 +52,9 @@ const App = () => {
);
};

ReactDOM.render(
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<StrictMode>
<App />
</StrictMode>,
document.getElementById('root')
</StrictMode>
);
20 changes: 10 additions & 10 deletions examples/advanced-scoped-flow/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import React, { Component, StrictMode } from 'react';
import ReactDOM from 'react-dom/client';
import { defaults } from 'react-sweet-state';

import { ChatRpc } from './views/chat-rpc';
Expand All @@ -10,11 +10,6 @@ import { ChatHook } from './views/chat-hook';
*/
defaults.devtools = true;

/**
* Enable Batch updates
*/
defaults.batchUpdates = true;

/**
* Main App
*/
Expand Down Expand Up @@ -57,19 +52,24 @@ class App extends Component<
key={String(remount)}
id={String(reset)}
remoteUsers={remoteUsers}
defaultColor="#FED"
defaultColor="#FFF"
/>
<hr />
<ChatHook
key={String(remount + 1)}
id={String(reset + 1)}
remoteUsers={remoteUsers}
defaultColor="#DED"
defaultColor="#EEE"
/>
</main>
</div>
);
}
}

ReactDOM.render(<App />, document.getElementById('root'));
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<StrictMode>
<App />
</StrictMode>
);
13 changes: 11 additions & 2 deletions examples/advanced-scoped-flow/views/chat-hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,22 @@ import { FormContainer, useForm } from '../components/form';
import { useMessagesActions, useMessagesValue } from '../components/messages';
import { ThemeContainer, useTheme } from '../components/theme';

const ThemeWrapper = React.memo(({ children }: any) => {
const IsolatedHeader = React.memo(() => {
const [{ color }, { change }] = useTheme();
return (
<div style={{ background: color }}>
<h3>With Hooks</h3>
<button onClick={() => change('#DFF')}>Theme 1</button>
<button onClick={() => change('#FDF')}>Theme 2</button>
<button onClick={() => change('#FFD')}>Theme 3</button>
</div>
);
});

const ThemeWrapper = React.memo(({ children }: any) => {
const [{ color }] = useTheme();
return (
<div style={{ background: color }}>
<h3>With Hooks</h3>
{children}
</div>
);
Expand Down Expand Up @@ -60,6 +68,7 @@ export const ChatHook: AbstractComponent<Props> = React.memo(
return (
<ThemeContainer scope={id} defaultColor={defaultColor}>
<ThemeWrapper>
<IsolatedHeader />
<MessagesList />
<FormContainer remoteUsers={remoteUsers}>
<FormComponent onSubmitSuccess={add} />
Expand Down
6 changes: 0 additions & 6 deletions examples/basic-flow/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
// @flow
import React from 'react';
import ReactDOM from 'react-dom';
import { defaults } from 'react-sweet-state';

import { useCounter } from './components';

/**
* Enable Batch updates
*/
defaults.batchUpdates = true;

/**
* Components
*/
Expand Down
11 changes: 8 additions & 3 deletions examples/basic-ts/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow
import React from 'react';
import ReactDOM from 'react-dom';
import React, { StrictMode } from 'react';
import ReactDOM from 'react-dom/client';

import { CounterSubscriber, useCounter } from './components';

Expand Down Expand Up @@ -41,4 +41,9 @@ const App = () => (
</div>
);

ReactDOM.render(<App />, document.getElementById('root'));
const root = ReactDOM.createRoot(document.getElementById('root')!);
root.render(
<StrictMode>
<App />
</StrictMode>
);
21 changes: 12 additions & 9 deletions examples/performance-scale-test/index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
// @flow
import React, { useEffect, useState } from 'react';
import ReactDOM from 'react-dom';
import { defaults } from 'react-sweet-state';
import React, { StrictMode, useEffect, useState } from 'react';
import ReactDOM from 'react-dom/client';

import { useTodo } from './controllers/todos';

/**
* Enable Batch updates
*/
defaults.batchUpdates = true;

const COLLECTION = Array.from({ length: 500 });

type TodoViewProps = { id: string, count: number };

const TodoView = ({ id, count }: TodoViewProps) => {
const [todo, actions] = useTodo({ id });

useEffect(() => {
if (!todo) actions.create(id);
}, [actions, id, todo]);

useEffect(() => {
actions.toggle(id);
}, [actions, count, id]);

return <div>{todo ? todo.title : 'creating...'}</div>;
};

Expand All @@ -42,4 +40,9 @@ const App = () => {
);
};

ReactDOM.render(<App />, document.getElementById('root'));
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<StrictMode>
<App />
</StrictMode>
);
52 changes: 15 additions & 37 deletions examples/performance-test/index.js
Original file line number Diff line number Diff line change
@@ -1,71 +1,49 @@
// @flow
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import { defaults } from 'react-sweet-state';
import React, { Component, StrictMode } from 'react';
import ReactDOM from 'react-dom/client';

import { TreeRpc } from './views/tree-rpc';
import { TreeHooks } from './views/tree-hooks';
import { Tree } from './views/tree';

/**
* Enable Batch updates
*/
defaults.batchUpdates = true;

const [TYPE = '', DEPTH = '2'] = window.location.hash
.replace('#', '')
.split('|');
const [DEPTH = '2'] = window.location.hash.replace('#', '').split('|');

/**
* Main App
*/
class App extends Component<{}, any> {
state = {
type: TYPE,
depth: Number(DEPTH),
};

onChangeType = (ev) => {
const { depth } = this.state;
const type = ev.target.value;
this.setState({ type });
window.location.hash = type + '|' + depth;
};

onChangeDepth = (ev) => {
const { type } = this.state;
const depth = Number(ev.target.value);
this.setState({ depth });
window.location.hash = type + '|' + depth;
window.location.hash = depth;
};

render() {
const { type, depth } = this.state;
const { depth } = this.state;
return (
<div>
<h1>Performance ({type})</h1>
Type:{' '}
<select onChange={this.onChangeType} value={type}>
{['', 'rpc', 'hooks'].map((v) => (
<option key={v} value={v}>
{v}
</option>
))}
</select>
<h1>Performance</h1>
Depth:{' '}
<select onChange={this.onChangeDepth} value={depth}>
{['2', '5', '10'].map((v) => (
{['0', '2', '5', '10'].map((v) => (
<option key={v} value={v}>
{v}
</option>
))}
</select>
<main>
{type === 'rpc' && <TreeRpc n={depth} />}
{type === 'hooks' && <TreeHooks n={depth} />}
<Tree n={depth} />
</main>
</div>
);
}
}

ReactDOM.render(<App />, document.getElementById('root'));
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<StrictMode>
<App />
</StrictMode>
);
Loading

0 comments on commit dba9676

Please sign in to comment.