0

Hi I have issue i cann't find the way how to change my component and send props to it. Actually I have Route in app.js like this:

....
 <Switch id="main-switch" className={'d-flex '}>

   <Route exact={true} path="/learning" component={Learning}/>

</Switch>
....

Now in other component I have other modal component inside. In this modal I have Button which should start the learning. Before that i have to get sama data from DB.... Actually I have all datas and last think what i need is to switch component with these data as prop... But... i don't know how to do this...

My method after clicking on startLearning Button

  startInteligentLearning(){
        const knowledgeIds = this.getIdsOfKnowledges();
        const includeLearned = this.state.disabledLearnedExercies;
        let data = null;

        this.getDataForLearning(knowledgeIds).then((d) => {
            data = d;
            console.log(data);
            console.log(includeLearned);

            this.props.history.push({
                pathname:"/learning",
                state:{
                    learningData:"data",
                    learningType:Constants.LEARNING_TYPE.Learning,
                    includeAlreadyLearned:includeLearned,
                }
            });
        });
    }

 async getDataForLearning(knowledgeIds){
        const startlearnResponse = await fetch('LearningController/StartLearning/', {
            method: 'POST',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({
                'UserRoleID': 1,
                'LearningType': Constants.LEARNING_TYPE.Learning,
                'KnowledgeIds': knowledgeIds,
            }),
        });

        const startLearnRes = await startlearnResponse.json();

        if (startlearnResponse.status === 200 && startLearnRes !== null) {
            return startLearnRes;
        }

    }

I tried use props.history in method above but i don't have history in my props... Is there other way how to change ?

Young L.
  • 922
  • 3
  • 13
  • 33
  • Can you use withRouter to wrap your component? https://stackoverflow.com/questions/43107912/how-to-access-history-object-in-new-react-router-v4 – Thakur Karthik May 19 '20 at 15:14
  • which chomponent? where is switch or these last one? – Young L. May 19 '20 at 15:19
  • If i am correct you are not able to change route on click of the button even though you are using history.push right? to get access to history object you need to wrap your component like when you are exporting it Ex:export default withRouter(ModalComponent). – Thakur Karthik May 19 '20 at 16:17
  • Also try to post some working code here for others to help for you.It is normally not easy to tell at first glance what the issue you are really facing is.Thanks ! – Thakur Karthik May 19 '20 at 16:19
  • Yes I alredy try this with this withRouter method. It works but it is not what i need... Problem is the props which i sent like this are stored in props.location.props.myCustomPropsWhichISent. With this i have to rewrite all my component to work with props like this instead props.myCustomPropsWhichISent. With this i have to rewrite all my component to work with props like this instead. Also after refreshing page page is broken and it throws me error uknown props from location.... But thx i will try to find solution... – Young L. May 19 '20 at 17:01

0 Answers0