score:1
Accepted answer
You can use ternary operator(?:) to achieve conditional rendering
render() {
return {this.state.validation == false?(
<form
onSubmit={
(e) => {
e.preventDefault();
this.handleSubmit(this.state.cityValue);
}
}
>
<input type="text" value={this.state.cityValue} onChange={this.handleChange} />
<input type="submit" value="Send" />
</form>):
(<App city={this.state.cityValue} />)
}}
score:1
Use Conditional Rendering.
You can try any of the below ways to implement conditional rendering in react:
- Using an if…else Statement
- Using a switch Statement
- Using Element Variables
- Using Ternary Operators
- Using Logical && (Short Circuit Evaluation)
- Using Immediately Invoked Function Expressions (IIFEs)
- Using Enhanced JSX Libraries
Refer Conditional Rendering in react docs: https://reactjs.org/docs/conditional-rendering.html
Source: stackoverflow.com
Related Query
- How do I properly render React components using conditionals if I need to show custom prompt first?
- How to render react components by using map and join?
- How can show and hide react components using hooks
- How to mock properly with Jest a React component using render props
- how do I render a page in react using functional components when onSubmit?
- How to show a component When I click a button using function components in react
- When we render react Components from an iterable using Array.prototype.map() how is the index generated, and how does react uses it?
- How to sort data in descending order and show in table, using functional components in React
- How can we show or hide components inside OverReact (library to build react components using dart ) according to conditions?
- How to render props in react components while using react-router-dom version 6
- How to import and export components using React + ES6 + webpack?
- How to render rectangles in a D3 grouped bar chart using React Faux DOM?
- Using for loops and switch cases in React to dynamically render different components
- How to show build datetime on my react web app using create-react-app?
- How to render a react element using an html string?
- How do I make components in React Native without using JSX?
- Swiper React | How to create custom navigation/pagination components using React refs?
- How Center Components Using Flex in React Native?
- How to show results of a map in two or more columns using react
- how to render a react component using ReactDOM Render
- How to pass the match when using render in Route component from react router (v4)
- React - How to iterate over dictionary in render method using JSX?
- No need for state in React components if using Redux and React-Redux?
- How to render images with React JS using map or loop?
- How to wait for complete render of React component in Mocha using Enzyme?
- How to render a React component (ES6 API) directly using ReactDOM?
- How I can render react components without jsx format?
- How to test functions other than render in a React class using jest?
- How does react render multiple pages in app.js using react-router?
- How to render React components only once on startup?
More Query from same tag
- Trouble installing Swiper in my React project
- How to write and access function in React stateless components
- How do I reset modal props on close
- Server responded with 404 - POST request
- Serverside component for Next.js
- Why a few this same components works differently in react?
- Microsoft clarity script throwing error in React js
- React Mobx - component not updating after store change
- React Native Context setUser is not a function
- Type InferableComponentEnhancerWithProps is missing the following properties from type Component<OwnProps>
- Is it good practice to put functions inside useEffect?
- remove the object from array by iterating the array on map function?
- Nested route is not working as expected - React router
- Uncaught SyntaxError: Unexpected token export
- Gatsby: Transforming data returned from a page query at build time
- React redux dispatch not available
- I am trying to store value of JSON data in an array from servlet but it shows posts.map is not function
- Express React i18n: Cannot GET /locales/resources.json (JS / TS / Node)
- Access react router dom history object outside React component
- Component not re-rendering , after reducer call and props not updating?
- disabling adjacent tabs using react hooks
- How to load Material UI Theme based on Redux Store entry
- Adding a link to a key press event
- How to use a firebase class in a js file
- react-redux redux-thunk: dispatch performance issue
- How to change State immutably React
- ReactJS CSS Class not working
- Reconnect WebSocket in react js
- NextJS dynamically add button onClick
- Correct way to get refs and attach event listeners in React ComponentDidMount?