score:2
Working app https://codesandbox.io/s/jovial-pascal-kww85?file=/src/App.js
Note:
- Prop name cannot be key as this name is reserved to uniquely identify component.(I have used apikey as name)
<Display apikey={key} placeName={placeName} />
- In Navbar you have to use another state to track of input in textbox.
const [input, setInput] = useState("");
- In Navbar it should be onClick={() => props.setPlaceName(input)}
value={input} onChange={(e) => setInput(e.target.value)}
- Form element on Submit prevent Default to not refresh page.
<form className="d-flex" onSubmit={(e) => e.preventDefault()}>
- In Display call useEffect when props.placename changes.
useEffect(async () => { getWeatherInfo(); }, [props.placeName]);
score:0
When a user clicks on the button you are not actually updating the state.
button onClick={props.setPlaceName}
should be something like
button onClick={() => props.setPlaceName("Hello world")}
score:0
The relevant part of your code is in the Navbar
component: you don't provide the new placeName
to the setter function.
So, for example, your Navbar
component should look somewhat like this:
function Navbar(props) {
// This state stores the updated input value
const [inputPlaceName, setInputPlaceName] = useState('');
// This function provides `setPlaceName` with the input value
function setGlobalPlaceName() {
props.setPlaceName(inputPlaceName);
}
return (
<form>
<input type="search" onChange={setInputPlaceName} />
<button onClick={setGlobalPlaceName} type="submit">
Search
</button>
</form>
);
}
Then, try to subscribe Display
component to updates of props.placeName
. This is done by adding it to the dependencies array of its useEffect
:
useEffect(getWeatherInfo, [props.placeName])
Source: stackoverflow.com
Related Query
- Component is not re rendering when state is changed in react
- React component not updating when store state has changed
- React component not getting rendered each time when state changed
- React component create a new instance of the state when re-rendering even the state is not changed
- React component not rerendring when state is changed with Redux
- React Component does not re rendering when updating an object in state hooks
- React component not refreshing when state changed
- React Component not re rendering when the state got updated
- React TypeScript - Component properties not being updated when parent state is changed
- React component not updating when state is changed
- React shouldComponentUpdate() is called even when props or state for that component did not change
- React memo keeps rendering when props have not changed
- React hooks - state in useState() is not reset when route is changed
- How to stop re render child component when any state changed in react js?
- Component rerenders when state has not changed on the second click
- React is not updating when Redux state is changed below the first level
- Component does not rerender but redux state has changed via react hooks
- React component not rendering when using react-rails gem
- React component not rendering on state update
- React component rendering even when there is no change in the state value
- Why React component get re-rendered only once when state does not change?
- In react class component why is the state not being changed in the first instance of button click? But in the second instance it gets updated?
- React Component not updated when state changes
- React JS - getting a separate component to re-render when localStorage state is changed
- react / redux toolkit useSelector not updating when store state changed
- How to re-rendering react class component when state is changed
- react onChange on text field update whole component when state is changed
- react router not rendering the component when using redux
- React component not re-rendering when "Date" in state gets updated
- Component doesn't re-render when Redux state is changed - React
More Query from same tag
- how to make click button in bootstrap and all data are come API
- antd SubMenu "TypeError: Cannot read property 'isRootMenu' of undefined"
- gsap timeline in react
- Typescript & React: Provide default value for discriminating union
- React function to only run once after re-visiting page
- Component not rendering fully and not changing with the change in State
- how to avoid undefined data when getting data from redux using useSelector?
- Why is my custom sort not working with Material-UI Data Grid component?
- React Component State issue with duplicate components
- User's context on isomorphic (or universal) react app with JWT
- How to implement form input validation in the react
- Windows.location.reload() not working in react js app hosted in hostgator
- React Higher Order Components to add a custom attribute to the rendered JSX
- How do I get the range offsets of an entire div in Javascript?
- Can I define breakpoint specific spacing to a custom Material-ui theme?
- filter option in form control select dropdown
- Can we use pure Bootstrap with React.js?
- Failed prop type The prop is marked as required but its value is `undefined`
- Why does React.js remove younger siblings?
- React list index is not correct
- How to get item from local storage?
- React js. How to make a component with this structure: <Component> description of the component </Component>
- ReactJs Blank page when I click on a link, but after I refresh the page contents show up
- How to reload a react component after an update using redux and hooks
- React.js this.props undefined unless page is refreshed
- How reliable are MUI Global Class names in JSS?
- Infinite loop when adding new element to an array React
- How can I pass props down in Angular 2+ like React?
- npm install throwing error related to ssl
- How to turn off rerender after dowload the file