Answer the question
In order to leave comments, you need to log in
How to change/select city in React.js with Redux in RoR?
Hello.
I am learning React with flux approach (Redux).
On the backend I have RoR.
Task: change the city and record it in the session.
Previously, on the view, I had this:
- city.each do |name|
= link_to name.city, controller: 'application', action: 'switch_city', city: name.city
def switch_city
session[:city] = params[:city] if params[:city]
redirect_to root_url
end
export function city(valueCity) {
...
}
export function city() {
return dispatch => {
dispatch({
type: 'LOAD'
});
axios.get(
Routes.root_path(), { headers: { 'Accept': 'application/json' } }
).then(result => {
dispatch({
type: 'LOAD_SUCCESS',
city: result.city,
})
})
}
}
export default function cityData(state, action) {
switch (action.type) {
...
case LOAD_SUCCESS:
return {
...state,
city: action.city,
};
default:
return state;
}
}
class App extends Component {
...
componentDidMount() {
...
dispatch( cityData() );
}
onLoadCity() {
...
dispatch( cityData() );
}
render() {
...
return (
<div>
<ComponentBar onClick={this.onLoadCity}/>
</div>
);
}
}
export default class ComponentBar extends Component {
render() {
return (
<div>
<button onClick={this.props.onClick}>
Выбрать город
</button>
</div>
);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question