0

I have a Form group that has multiple possible values. This selected value is then sent as a prop to another component so that it can be used in the other component. The problem I am getting is that when another element is selected, the other component doesn't re render with the new prop value.

This is the part of the code I am referring to:

import OtherComponent from './OtherComponent ';

function Body(){

   
   const [selectedStreet, setSelectedStreet] = useState("11th ave n ganservoort - 12th ave @ 40th st");

   return (
       <div>

           <Form className="mt-5">
               <Form.Group controlId="exampleForm.SelectCustom">
                   <Form.Label>Select Street</Form.Label>
                   <Form.Control as="select" onChange={ (event) =>{
                       setSelectedStreet(event.target.value)
                       // console.log(event.target.value)
                       // console.log(selectedStreet)
                   }} custom>
                       {dataToday.map(s => (
                               <option key={s["street"]} value={s["street"]}>{s["street"]}</option>
                       ))}
                       

                   </Form.Control>
               </Form.Group>
           </Form>
           <OtherComponent streetname={selectedStreet}></OtherComponent >


       </div>


          
   );
}

export default Body;

The options inside the form group are just a list of strings. I want to know how to be able to call again the component once the user changes the value of the form group.

This is the OtherComponent:


import { Bar, Line, Pie } from 'react-chartjs-2';
import React, { useState } from 'react';

function OtherComponent(props) {

   console.log(props.streetname);

   const [barData, setBarData] = useState({
       labels: ['label 1', 'label 2', 'label 3', 'label 4'],
       datasets: [
           {
               label: props.streetname,
               data: [
                   48,
                   35,
                   73,
                   82
               ],
               backgroundColor: [
                   'rgba(255, 99, 132, 0.6)',
                   'rgba(54, 162, 235, 0.6)',
                   'rgba(255, 206, 86, 0.6)',
                   'rgba(75, 192, 192, 0.6)'
               ],
               borderWidth: 3
           }
       ]
   });

   // set options
   const [barOptions, setBarOptions] = useState({
       options: {
           scales: {
               yAxes: [
                   {
                       ticks: {
                           beginAtZero: true
                       }
                   }
               ]
           },
           title: {
               display: true,
               text: props.streetname,
           },
           legend: {
               display: true,
               position: 'top'
           }
       }
   });

   // return JSX
   return (
       <div className="BarExample">
           <Bar
               data={barData}
               options={barOptions.options} />
       </div>
   );
}


  • What does `OtherComponent` look like? – Matt U Jun 21 '21 at 20:18
  • Just updated the post showing the other component. It's basically the code shown in this url: https://wizardly-gates-b3c3b3.netlify.app/ It is about how to create a bar chart. My idea is that the bar chart will be changed when the user selects another option. – ULPGCstudent Jun 21 '21 at 20:43
  • Does this answer your question? [How to sync props to state using React hooks : setState()](https://stackoverflow.com/questions/54625831/how-to-sync-props-to-state-using-react-hooks-setstate) – Matt U Jun 21 '21 at 20:54
  • The linked answer talks about how to update a component's state (`OtherComponent` in your case) based on a prop change. – Matt U Jun 21 '21 at 20:54

0 Answers0