我正在尝试使用useSelector和useDispatch更改bulma模型的状态
就像这样
const isState = useSelector((state) => state.isActiveState)
Model.js为:
import React from 'react'
import "../CSS/Careers.css"
import { useSelector, useDispatch } from 'react-redux';
import { stateCheck } from '../Redux/ActiveState'
export default function Modal() {
const isState = useSelector((state) => state.isActiveState)
const dispatch = useDispatch()
return (
<div>
<div
style={{ padding: "0rem 0.5rem 0rem" }}
className={`modal ${isState}`} //this should change the state to 'is-active'
>
<div onClick={() => dispatch(stateCheck())} className="modal-background"></div>
<div style={{ borderRadius: "1.5rem" }} className="modal-card">
<header
style={{
borderBottom: "1px solid white",
backgroundColor: "#F2F5FF",
}}
className=""
>
<div style={{ padding: "17px 19px 20px" }} className="is-flex ">
<p
style={{ color: "#7209B7" }}
className="modal-card-title has-text-weight-semibold"
>
Apply For Job
</p>
<button
onClick={() => dispatch(stateCheck())}
className="delete"
aria-label="close"
></button>
</div>
</header>
<section
style={{ backgroundColor: "#F2F5FF" }}
className="modal-card-body"
>
<div style={{ padding: "0rem 3rem 0rem" }} className="field">
<div className="control has-icons-left ">
<input
className="input "
// style={{ width: "100%" }}
type="text"
placeholder="Name"
/>
<span className="icon is-small is-left">
<i className="fas fa-user"></i>
</span>
</div>
</div>
<div style={{ padding: "0rem 3rem 0rem" }} className="field">
<div className="control has-icons-left ">
<input className="input " type="email" placeholder="Email" />
<span className="icon is-small is-left">
<i className="fas fa-envelope"></i>
</span>
</div>
</div>
<div
style={{ padding: "0rem 3rem 0rem" }}
className="file is-medium"
>
<label style={{ border: "3px sold #7209B7" }} className="">
<input className="file-input" type="file" name="resume" />
<span
style={{ backgroundColor: "#F2F5FF" }}
className="file-cta"
>
<span className="file-icon">
<i
style={{ color: "#7209B7" }}
className="fas fa-upload"
></i>
</span>
<span style={{ color: "#7209B7" }} className="file-label">
Choose a file…
</span>
</span>
</label>
</div>
<p
style={{
padding: "0rem 3rem 0rem",
fontSize: "15px",
color: "#fcb01a",
}}
>
Select CV or Resume
</p>
</section>
<footer
style={{
borderTop: "1px solid white",
textAlign: "center",
height: "20%",
backgroundColor: "#F2F5FF",
}}
className=" has-text-centered"
>
<div
style={{ paddingTop: "9px", textAlign: "center" }}
className=""
>
<button style={{ backgroundColor: "#fcb01a" }} className="button">
Submit
</button>
</div>
</footer>
</div>
</div>
</div>
)
}
我的redux文件ActiveState.js是:
import { createSlice } from '@reduxjs/toolkit'
export const ActiveState = createSlice({
name: 'isActiveState',
initialState: {
value: 0,
},
reducers: {
stateCheck: (state) => {
// Redux Toolkit allows us to write "mutating" logic in reducers. It
// doesn't actually mutate the state because it uses the Immer library,
// which detects changes to a "draft state" and produces a brand new
// immutable state based off those changes
if (state.value == 0){
state.value = 'is-active';
// console.log(state.value)
}
else{
state.value = 0;
// console.log(state.value)
}
}
},
})
export const { stateCheck } = ActiveState.actions;
export default ActiveState.reducer;
并且store.js为:
import { configureStore } from '@reduxjs/toolkit'
import ActiveState from './components/Redux/ActiveState'
export default configureStore({
reducer: {
stateChecker : ActiveState,
},
})
索引. js为:
import React from 'react';
import ReactDOM from 'react-dom';
import './components/CSS/index.css';
import App from './App';
import store from './store'
import { Provider } from 'react-redux'
ReactDOM.render(
<React.StrictMode>
<Provider store={store}>
<App />
</Provider>
</React.StrictMode>,
document.getElementById('root')
);
我已经将model添加到组件中,并在组件中使用它,以触发我使用的model:
onClick={() => dispatch(stateCheck())}
调度工作正常,我已使用console.log进行检查
问题是,当我尝试使用以下语句获取redux状态时:
const isState = useSelector((state) => state.isActiveState)
我在控制台中收到以下错误:
Uncaught TypeError: dispatcher.useSyncExternalStore is not a function
我正在跟踪redux官方文档:
https://react-redux.js.org/tutorials/quick-start
我已经尝试了一切,检查导入,检查语法和关键字,并检查了StackOverflow上的每一个答案,但我仍然得到这个错误。
请帮帮我我被困在这里快一天了。
5条答案
按热度按时间uttx8gqw1#
发生这种情况是因为React 18**(React和React Native)尚不支持
react-redux
**。将
react-redux
降级到以前的版本将解决您的问题。对我来说,我的React Native项目降级了:
至
lhcgjxsq2#
完整代码
索引.js
模态.js
切片.js
存储.js
它不是isActiveState。您应该使用在configureStore中指定的reducer的名称。
stateChecker
现在isState将具有
value
。qq24tv8q3#
为什么要使用“ReactDOM.render”?您使用了React18附带一种名为useSyncExternalStore的技术。您不需要使用ReactDOM.createRoot(元素,选项).render(组件)吗?
ewm0tg9j4#
将React18降级为React17
eimct9ow5#
我有同样的问题,因为我有
"react-dom": "^17.0.1"
,而Reactv18安装。我的设置是这样的:我将设置切换为:
它工作。包版本