I have job details data in my Context.js which will be the global data for my application. I want to use that data inside my Search.js component and categorize in a certain manner. How can I do that?
Here is my code
Context.js will looks like this.
import React, { Component } from 'react';
export const allJobs = [{
id : 1,
company : 'Google',
position: 'Software Engineer',
industry: 'Information Technology',
location: 'Bangalore',
rating: '4.5',
label: 'Women Safety'
},
{
id : 2,
company : 'Axis',
position: 'Account Manager',
industry: 'Banking',
location: 'Delhi',
rating: '3.8',
label: 'Salary & Benefits'
},
{
id : 3,
company : 'Cisco',
position: 'HR Manager',
industry: 'Information technology',
location: 'Pune',
rating: '3.5',
label: 'Learning opportunities'
},
{
id : 4,
company : 'Accenture',
position: 'Project Manager',
industry: 'Information technology',
location: 'Bangalore',
rating: '4.1',
label: 'Female Representatives'
},
{
id : 5,
company : 'Republic',
position: 'News Reader',
industry: 'Media',
location: 'Delhi',
rating: '3.2',
label: 'Maternity Leaves'
},
]
export const Context = React.createContext({
jobs: allJobs
});
export class Provider extends Component {
render(){
return(
<Context.Provider value={allJobs}>
{this.props.children}
</Context.Provider>
)
}
}
this will be `Search.js component
import React, { Component } from "react";
import {Context, allJobs} from '../Context';
class Search extends Component {
constructor(props){
super(props);
this.state = {
jobs: allJobs,
options: [],
};
}
componentDidMount(){
let tempArr =[];
this.state.jobs.forEach(el=>{
// tempArr = [...tempArr, el.company];
tempArr = [
...tempArr,
{companies: {value : el.company, label : el.company}}
{industries: {value : el.industry, label : el.industry}}
];
})
this.setState({
options: tempArr
});
}
triggerSwitch = (value) => {
switch (param) {
case "companies":
return (
<h1>Companies</h1>
<h5>this.state.options.companies</h5>
);
case "industries":
return (
<h1>Industries</h1>
<h5>this.state.options.industries</h5>
);
default:
return console.log("companies");
}
};
render(){
return(
<Context.Consumer>
{value => {
return(
{this.triggerSwitch( value)}
)}}
</Context.Consumer>
)
}
}
export default Search;
Thanks in advance.
Front-End Engineer
What you've done looks to be right to me as long as:
Context.Provider value is defined somewhere in the top of the hierarchy and Search component should be present in the same component tree but somewhere below the Provider in the hierarchy.defaultValue and value of the Context should be same.value inside the Consumer will have allJobs structure to be consumed. I'm assuming your logic of triggerSwitch(value) takes care of that (because currently the syntax is invalid).
Diego Bernal
Would this work for you? I would set the
valueproperty to what ever you will use to make the assignmentcomponentDidMount(){ const{jobs} = this.state; const options = jobs.map(job => { return Object.assign({}, {value: job.id, label: job.position}) }) this.setState({options}) console.log("OPTIONS", options); } /** options is: [ { value: 1, label: "Software Engineer" }, { value: 2, label: "Account Manager" }, { value: 3, label: "HR Manager" }, { value: 4, label: "Project Manager" }, { value: 5, label: "News Reader" } ] */