Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
405 views
in Technique[技术] by (71.8m points)

reactjs - React-Redux Redux Action getting called multiple Times

I am new to React-Redux and I have a problem with one of my redux action objects. When I print my function this.handleUserByID(1) @KeyboardPage.jsx to console everything gets called multiple times and I recieve this error: Error_User_BYID

But I still get the right result in the console at some point:

[success_USER_BYID2

Here my Code:

KeyboardPage.jsx

class KeyboardPage extends React.Component {

componentDidMount() {
    this.props.getTopics(2);
}

handleUserByID(user_id){
    return this.props.getUser(user_id);
}

render() {

    const { topics } = this.props;
    const { user, users} = this.props;

    console.log(this.handleUserByID(1));

    return (
        <div className="container">
            <NavBar loggedinAs={user.user_name}/>
            <h1>Gruppe 1 - Webforum</h1>
            <h3>Topics :</h3>
            {topics.loading && <em>Loading topics...</em>}
            {topics.error && <span className="text-danger">ERROR: {topics.error}</span>}
            {topics.items && users.items &&
            <ul type='none' >
                {topics.items.map((topic, index) =>
                    <li key={topic.topic_id}>
                        <p/>
                        <a href={"/" + topic.topic_id} style={{ textDecoration: 'none' }}>
                            <TopicCard topicSubject={topic.topic_subject} topicDate={topic.topic_date} topicBy={this.handleUserByID(topic.topic_by).user_name}/>
                        </a>
                    </li>
                )}
            </ul>
            }

        </div>
    );
}
}


function mapState(state) {
    const {topics } = state;
    const { users, authentication } = state;
    const { user, } = authentication;
    return { user, users, topics};
}

const actionCreators = {
    getTopics: topicActions.getAllbyID,
    getUser: userActions.getById
}

const connectedKeyboardPage = connect(mapState, actionCreators)(KeyboardPage);
export { connectedKeyboardPage as KeyboardPage };

users.reducer.js

export function users(state = {}, action) {
    switch (action.type) {
        case userConstants.GETBYID_REQUEST:
            return {
                loading: true, id : action.id
            };
        case userConstants.GETBYID_SUCCESS:
            return {
                item: action.user
            };
        case userConstants.GETBYID_FAILURE:
            return {
                error: action.error
            };
        default:
            return state
    }
}

user.service.js

function getById(id) {
const requestOptions = {
    method: 'GET',
    headers: authHeader()
};

return fetch(`${config.apiUrl}/users/${id}`, requestOptions).then(handleResponse);
}

user.actions.js

function getById(id) {

    return dispatch => {
        dispatch(request(id));

        userService.getById(id)
            .then(
                user => dispatch(success(user)),
                error => dispatch(failure(error.toString()))
            );
    };

    function request(id) { return { type: userConstants.GETBYID_REQUEST, id} }
    function success(user) { return { type: userConstants.GETBYID_SUCCESS, user } }
    function failure(error) { return { type: userConstants.GETBYID_FAILURE, error } }
}

root reducer

import { combineReducers } from 'redux';

import { authentication } from './authentication.reducer';
import { registration } from './registration.reducer';
import { users } from './users.reducer';
import { categories } from './categories.reducer';
import { topics } from './topics.reducer';
import { alert } from './alert.reducer';

const rootReducer = combineReducers({
    authentication,
    registration,
    users,
    categories,
    topics,
    alert
});

export default rootReducer;

EDIT: I found a way to solve the problem. In my component KeyboardPage.jsx I already retrieved all the users. I I now simply filter the users directly according to the userId. Not the most elegant solution, but it works for now.

        {topics.items && users.items &&
        <ul type='none' >
            {topics.items.map((topic, index) =>
                <li key={topic.topic_id}>
                    <p/>
                    <a href={"/" + topic.topic_id} style={{ textDecoration: 'none' }}>
                        <TopicCard topicSubject={topic.topic_subject} topicDate={topic.topic_date} topicBy={users.items.filter(user => user.user_id === topic.topic_by)[0].user_name}/>
                    </a>
                </li>
            )}
        </ul>
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...