やっていること
RustのGUIフレームワーク、Tauriを使用してGUIを書いています。
ReactとReduxを使い、GUI(タブ分割)を実現したいと思っています。
やりたいこと
ボタンをクリックしたらRust側でカウントを1増やす。
UI側でタブを別タブに切り替える
戻したときにUIのステートが切り替える前と同じ状態に戻っていること
コード
rust
#![cfg_attr( all(not(debug_assertions), target_os = "windows"), windows_subsystem = "windows" )] fn main() { let context = tauri::generate_context!(); tauri::Builder::default() .invoke_handler(tauri::generate_handler![countup,]) .menu(tauri::Menu::os_default(&context.package_info().name)) .run(context) .expect("error while running tauri application"); } #[tauri::command] fn countup(count: i32) -> i32 { println!("{}", count + 1); count + 1 }
UI側
package.json
{ "name": "tauri-app", "version": "0.1.0", "private": true, "dependencies": { "@reduxjs/toolkit": "^1.8.2", "@tauri-apps/api": "^1.0.1", "@testing-library/jest-dom": "^5.16.4", "@testing-library/react": "^13.3.0", "@testing-library/user-event": "^13.5.0", "@types/jest": "^27.5.2", "@types/node": "^16.11.41", "@types/react": "^18.0.14", "@types/react-dom": "^18.0.5", "@types/react-redux": "^7.1.24", "react": "^18.2.0", "react-dom": "^18.2.0", "react-redux": "file:types/react-redux@reduxjs/toolkit", "react-scripts": "^5.0.1", "react-tabs": "^5.1.0", "typescript": "^4.7.4", "web-vitals": "^2.1.4" }, "scripts": { "start": "cross-env BROWSER=none react-scripts start", "build": "react-scripts build", "test": "react-scripts test", "eject": "react-scripts eject", "tauri": "tauri" }, "eslintConfig": { "extends": [ "react-app", "react-app/jest" ] }, "browserslist": { "production": [ ">0.2%", "not dead", "not op_mini all" ], "development": [ "last 1 chrome version", "last 1 firefox version", "last 1 safari version" ] }, "devDependencies": { "@tauri-apps/cli": "^1.0.0", "cross-env": "^7.0.3" } }
App.tsx
import React from 'react'; import './App.css'; import './tab_style.css' // import 'react-tabs/style/react-tabs.css'; import { invoke } from '@tauri-apps/api'; import { Tab, Tabs, TabList, TabPanel } from 'react-tabs'; import { TabPage1 } from './TabPages/TabPage1' class App extends React.Component { render(): React.ReactNode { return ( <div className="App"> <TabUI></TabUI> </div> ); } } class TabUI extends React.Component { state = { value: 0, }; handleChange = (value: any) => { this.setState({ value }); console.log(value) }; render() { const { value } = this.state; return (<div id="react_tab_conainer"> <Tabs value={value} onChange={this.handleChange}> <TabList> <Tab>Counter</Tab> <Tab>Bar</Tab> <Tab>Baz</Tab> </TabList> <TabPanel> <TabPage1></TabPage1> </TabPanel> <TabPanel> {Page2()} </TabPanel> <TabPanel> Baz </TabPanel> </Tabs> </div > ) } } function Page2() { return (<div className="panel-content"></div>) } export default App;
store.tsx
import { configureStore, createSlice } from '@reduxjs/toolkit' import { TabPage1, TabPage1Slice } from './TabPages/TabPage1' const store = configureStore({ reducer: TabPage1Slice.reducer });
import React from 'react'; import './../App.css'; import './../tab_style.css' // import 'react-tabs/style/react-tabs.css'; import { invoke } from '@tauri-apps/api'; import { configureStore, createAsyncThunk, createSlice, PayloadAction } from '@reduxjs/toolkit' import { useSelector, useDispatch } from 'react-redux'; // 非同期 const clickAsync = createAsyncThunk( "rust/count", async (count: number) => { return await invoke("countup", { count: count }); } ); export const TabPage1Slice = createSlice({ name: 'TabPage1Slice', initialState: { counter: 0 }, reducers: {}, extraReducers: (builder) => { builder.addCase(clickAsync.fulfilled, (state, action: PayloadAction<any>) => { state.counter = action.payload; }); } }); export class TabPage1 extends React.Component<{}, { counter: number }>{ constructor(props: any) { super(props) this.state = { counter: 0 } } render(): React.ReactNode { const dispatch = useDispatch(); return (<Counter handleClick={() => { dispatch(clickAsync(this.state.counter)) }} counter={this.state.counter} > </Counter>) } } function Counter(props: any) { return ( <div className="panel-content" > <button onClick={props.handleClick}> Click</button> <label> {props.counter} </label> </div> ) }
発生しているエラー
ERROR [eslint] src\TabPages\TabPage1.tsx Line 40:26: React Hook "useDispatch" cannot be called in a class component. React Hooks must be called in a React function component or a custom React Hook function react-hooks/rules-of-hooks Search for the keywords to learn more about each error. ERROR [eslint] src\TabPages\TabPage1.tsx Line 40:26: React Hook "useDispatch" cannot be called in a class component. React Hooks must be called in a React function component or a custom React Hook function react-hooks/rules-of-hooks Search for the keywords to learn more about each error. ERROR in src/TabPages/TabPage1.tsx:41:56 TS2345: Argument of type 'AsyncThunkAction<RejectWithValue<unknown, unknown>, number, {}>' is not assignable to parameter of type 'AnyAction'. 39 | render(): React.ReactNode { 40 | const dispatch = useDispatch(); > 41 | return (<Counter handleClick={() => { dispatch(clickAsync(this.state.counter)) }} counter={this.state.counter} > </Counter>) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 42 | } 43 | } 44 |
TypeScriptとReactが分からなさすぎてエラーを読んでも理解が出来ません。
具体的に何をどうしたらいいのでしょうか?
すごくふわっとしていて申し訳ないんですが、宜しくお願いします。
まだ回答がついていません
会員登録して回答してみよう