Help on Map State to Props topic

Tell us what’s happening:
hey, please help. i dont know how to return an object that maps the state to specific property names.

Your code so far


const state = [];

// change code below this line
function mapStateToProps(state)
{
    return Object.assign(props, state);

}

Object.assign(mapStateToProps(state), { messages: [...state]});

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:66.0) Gecko/20100101 Firefox/66.0.

Link to the challenge:
https://learn.freecodecamp.org/front-end-libraries/react-and-redux/map-state-to-props

why are you using Object.assign ? and what is the value of props and why are you using it?

from the lesson

This function should take state as an argument, then return an object which maps that state to specific property names. These properties will become accessible to your component via props

And what should that returned object contain please
??

You have 3 basic steps to fulfill:

  1. Create a function mapStateToProps() . This function should take state as an argument :heavy_check_mark:
  2. return an object which maps that state to specific property names. :x:
  3. Create a property messages in the object that’s being returned, and set it to state . :x:

You have step one, delete what you are returning and remove the Object.assign too and try to fulfill step 2, post what you have tried here

const state = [];

// change code below this line
function mapStateToProps(state)
{
    let obj = {};
     state.forEach((item) => obj.add(item));
    return obj;

}

let objet = mapStateToProps(state);
objet.messages = state;

This is what i get dear. But it’s wrong. what to do please?

You are overly complicating it, hint for step 2

Hint: Here is an object that maps countrries to property name (continents)

{
   North_America: "USA",
   Africa: "Ethiopia"
}

const state = [];

// change code below this line
function mapStateToProps(state)
{
   return {
       tab: state
   };
}

let objet = mapStateToProps(state);

objet.messages = state;

Look at this sir. i returned an object from that function. what could going wrong here
?

Ok you are getting close but why do you have this

let objet = mapStateToProps(state);

objet.messages = state;

and what is the tab property where are you getting it from ?

const state = [];

// change code below this line
function mapStateToProps(state)
{
   return {
       state
   };
}

let objet = mapStateToProps(state);

objet.messages = state;

it is said from this mapTostaeprops.messages is undefined sir. why?

You need to only fulfill what the problem asked from you, again the steps are clear, you do not need anything outside of the function, the function itself is to return an object, the object should have a messages property and a state value, that is it.

const state = [];

// change code below this line
function mapStateToProps(state)
{
   return {
       messages: state
   };
}

let objet = mapStateToProps(state);

this is what i tried now and it works. i mapped the state to the messages property, all of this within the returned object sir. THanks. is there more explanation?

This part is not needed at all.

Well @RandellDawson i understood. was just for the tests with console.log

1 Like