0

i have a component child that is used in create and edit mode. it has select menu that displays no value until user selects an option from select menu in create mode.

in edit mode, this select menu should show the previously selected option.

below is my code,

const Child = (mode: mode, formikBag: formikBag) => {
    const handleValue = (field) => {
        if (mode === 'edit') {
            const selectedId = formikBag.values[itemId];
            const selectedItems = allItems.filter((item) => item.id === 
            itemId;
            return selectedItem[0].name;
        }
        return allItems.filter((option) => option.value === field.value); 
        //should be returned in create mode
    return(
        <Select
            value={handleValue}
        />
    );
}

the above code doesnt show any value in edit or create mode. not sure what is going wrong. i am new to react.

could someone help me with this. thanks.

1
  • did you tried value={() => handleValue('fieldName')} ? Commented Feb 1, 2021 at 7:39

1 Answer 1

2

handleValue is a function, you need to execute it in order to get the return for passing down

<Select value={() => handleValue(yourFieldHere)} />

If Select is the component you write, you can call the function inside Select

//In Select.js

//{value} means deconstruct that specific props passed from the parent, which name is 'value'. In this case, you have passed the handleValue through value. Therefore, you can use that function in the Select component
const Select = ({value}) => {
    const selectValue = value(yourFieldName)

    console.log(selectValue)
    return (
       <div>
         //some content
       </div>
    )
}

export default Select
Sign up to request clarification or add additional context in comments.

5 Comments

thanks for some reason it is not executing the method handleValue. i have some log statements once it starts executing handleValue. it doesnt log them
@stackuser Which way are you using? Executing the function on Select or inside Select ??
executing function inside select
When you executing inside Select, you still need to pass your function from the parent. How did you passed the function down to Select?
i see that the select component takes value of form { label: 'something', value: 'value',}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.