I get this string from an api [10, 20, 22, 26]. These values are assigned to [A, B, C, D] and I have a slider that has these values [1, 2, 3 ,4].
What I need is when you select 1 in the slider, it gives you the value of A (which is 10 in this case). If you select 2 in the slider it should give you the value of B (which is 20), and so on.
i can do this when i mock the data const valuesFromApi = [ 10,20, 22]; but i cant do the same with the data from my api, i guess that is because its a string and not an array.
this is my code
const Calculadora = ({navigation}) => {
const [sliderValue, setsliderValue] = useState();
const [sliderValue2, setsliderValue2] = useState(A);
const [A, setvA] = useState();
const [B, setvB] = useState();
const [C, setvC] = useState();
const [D, setvD] = useState();
const [pterms, setpterms] = useState([]);
const valuesFromApi = [ 10,20, 22];
const [value, setValue] = useState(valuesFromApi[0]);
this is how i get the data from api
useEffect(() => {
async function BCcontroller() {
const vCreationUser = 6;
const vSolicitudeId = 8;
const { data } = await ForceApi.post(`/ConsultBCController.php`, {vSolicitudeId, vCreationUser});
const values = data.terms;
setpterms(data.terms);
console.log(setpterms);
const [termA, termB, termC, termD] = values.split(',');
setvA(Number(termA));
setvB(Number(termB));
setvC(Number(termC));
setvD(Number(termD));
}
BCcontroller();
}, []);
and this is my slider
<View style={{alignItems: 'stretch', justifyContent: 'center' }}>
<Slider
maximumValue={D > 0 ? 4: 3 }
minimumValue={1}
step={1}
value={pterms.indexOf(value)}
onValueChange={index => setValue(pterms[index])}
/>
<View style={styles.plazos}>
<Text style={styles.plazo1}>{A} meses</Text>
<Text style={styles.plazo2}>{B} meses</Text>
<Text style={styles.plazo3}>{C} meses</Text>
{D > 0 ? <Text style={styles.plazo3}>{D} meses</Text>: null }
</View>
<Text style={styles.slideText}>Su credito por: ${A}MXN</Text>
<Text style={styles.slideText}>Usted recibe: ${A}MXN</Text>
<Text style={styles.slideText}>A un plazo de: {sliderValue2} meses</Text>
<Text style={styles.PaymentText}>Su pago: ${A}.00 MXN</Text>
</View>
any helpr would be appreciated
JSON.parse()to turn your string into an array./ConsultBCController.php, {vSolicitudeId, vCreationUser}); const values = data.terms; const obj =JSON.parse(values); and it throwme undefined'') values. As being said, you may share response sample, so I'll give complete answer. Otherwise, there's a huge room to make all sorts of guesses.