Actually, I think you’re riding it wrong.
I will try to do a step by step for you. The error is not in useSelector. You are using it correctly. The problem is that what useSelector is trying to select is not a store state. The setting is wrong. There’s a whole structure behind it, you see, I hope you can do it, if you already know and the mistake is not this I apologize.
First, you must get your main component involved by the context component of React-Redux, usually the main component is index.js.
index js.
import React from 'react';
import ReactDOM from 'react-dom';
import App from './app';
ReactDOM.render(<App />, document.getElementById('root'));
app js.
import { Provider } from "react-redux";
import Store from "./store";
import OutroComponente from './components/outro';
const App = () => {
return (
<Provider store={Store}>
<OutroComponente />
</Provider>
);
};
export default App;
Provider needs the store configuration that is in the store.js file requested above. See the code for the store.js file
store js.
import { createStore } from 'redux';
import dataReducer from './dataReducer';
const Store = createStore(combineReducers({
data: dataReducer,
// Outros state caso precise aqui
}
);
export default Store;
I just created a state store called data since that’s what you expect, but then you can add as many state you want. It is this name that you bind through the useSelector in the component.
Now you need to configure the date reset, I left in another file also for organization. It would look like this:
dataReducer.js
import { LOAD_SCHEDULED_SERVICES } from '../actions/actionTypes';
const initialState = 'Please show up on the component'
const dataReducer = (state = initialState, action) => {
switch (action.type) {
case SET_DATA:
return action.payload;
default:
return state;
}
};
export default dataReducer;
SET_DATA is used with Hooks useDispatch to change the store value and the useSelector to fetch the store value.
So, in any component that is now, if you want to get the value of the store state data is enough:
const text = useSelector(state => state.data)
I hope I helped you, anything, if I messed up what I wanted to say I apologize. Thanks bro
Oops, sorry for the delay, but I was able to solve the problem, and it was an intermediate step, to access the component where I use the selector it goes through another component and this component was not connected with Redux, it was just connect it that worked all right :D
– Itoh