How can I convert class component to functional component? - reactjs

I have a class based component, and I want to convert to the functional based component, my code is below:
import { enableRipple } from '#syncfusion/ej2-base';
import { DropDownButtonComponent } from '#syncfusion/ej2-react-splitbuttons';
import * as React from 'react';
import * as ReactDom from 'react-dom';
enableRipple(true);
// To render DropDownButton.
class App extends React.Component {
constructor() {
super(...arguments);
this.items = [
{
text: 'Display Settings'
},
{
text: 'System Settings'
},
{
text: 'Additional Settings'
}
];
}
render() {
return (<div>
<DropDownButtonComponent items={this.items} iconCss='e-icons e-image' cssClass='e-caret-hide'/>
</div>);
}
}
ReactDom.render(<App />, document.getElementById('button'));
But I did not get it how to convert this part below, any idea?
constructor() {
super(...arguments);
this.items = [
{
text: 'Display Settings'
},
{
text: 'System Settings'
},
{
text: 'Additional Settings'
}
];
}

This could be converted to functional component as bellow:
import { enableRipple } from '#syncfusion/ej2-base';
import { DropDownButtonComponent } from '#syncfusion/ej2-react-splitbuttons';
import * as React from 'react';
import * as ReactDom from 'react-dom';
enableRipple(true);
const App = () => {
const items = [
{
text: 'Display Settings',
},
{
text: 'System Settings',
},
{
text: 'Additional Settings',
},
];
return (
<div>
<DropDownButtonComponent
items={items}
iconCss="e-icons e-image"
cssClass="e-caret-hide"
/>
</div>
);
};
ReactDom.render(<App />, document.getElementById('button'));

Related

dynamic ionic icon with react typescript

I want to make my IonIcons dynamic so they are reusable. But i need to set it in {} I dont know how i do this with a .map() element.
import React from "react";
import { IonIcon, IonLabel } from "#ionic/react";
import styles from "./quickOptions.module.css";
import { alarmOutline, personOutline, cartOutline } from "ionicons/icons";
export default function quichOptions() {
const quickOptions = [
{ title: "Jouw consulent", icon: { personOutline } },
{ title: "Jouw afspraken", icon: { alarmOutline } },
{ title: "Jouw bestellingen", icon: { cartOutline } },
];
return (
<div className={styles.mainContainer}>
{quickOptions?.map((element: any) => {
return (
<div key={element.title} className={styles.btnContainer}>
<IonLabel>{element.title}</IonLabel>
<IonIcon icon={element.icon} size="large" /> //here
</div>
);
})}
</div>
);
}
Element.icon does not give the output of {personOutline} for example does anybody know how to fix this??
you can check console.log(typeof element.icon)
const quickOptions = [
{ title: "Jouw consulent", icon: 'personOutline' },
];
<IonIcon icon={element.icon} size="large" />
if the icon type here is {string}, that's why it doesn't work
try it in quickOptions , icon: personOutline or icon: 'personOutline'

react material-ui mui-datatables onRowSelectionChange ID

This is my react code. I am using the material UI. I am working with ID related events. the full code is provided below.
Here, the index ID is getting automatically generated. The issue has to do with that.
import React, { useState } from "react";
import ReactDOM from "react-dom";
import MUIDataTable from "mui-datatables";
import InputLabel from "#material-ui/core/InputLabel";
import MenuItem from "#material-ui/core/MenuItem";
import FormHelperText from "#material-ui/core/FormHelperText";
import FormControl from "#material-ui/core/FormControl";
import Select from "#material-ui/core/Select";
function Ag() {
const [responsive, setResponsive] = useState("vertical");
const onCellClick = () => {
console.log("sadf");
};
const onRowsDelete = () => {
console.log("remove");
};
const onRowSelectionChange = (ev, ex, ez) => {
console.log(ez);
};
const columns = ["Name", "Title", "Location"];
const options = {
filter: true,
filterType: "dropdown",
responsive,
onCellClick,
onRowsDelete,
onRowSelectionChange,
};
const data = [
{
Id: "1",
Name: "sunder",
Title: "dlamds",
Location: "asdfsa",
},
{
Id: "2",
Name: "cvzx",
Title: "sadfsda",
Location: "sadfsdacv",
},
{
Id: "3",
Name: "dsfas",
Title: "werq",
Location: "ewqrwqe",
},
{
Id: "4",
Name: "wqer",
Title: "gfdsg",
Location: "bvcxb",
},
{
Id: "5",
Name: "ereq",
Title: "qwer",
Location: "sdafas",
},
];
return (
<React.Fragment>
<MUIDataTable
title={"ACME Employee list"}
data={data}
columns={columns}
options={options}
/>
</React.Fragment>
);
}
export default Ag;
I want to get a data ID instead of an index ID that was automatically generated when I clicked.
What should I do?
onRowSelectionChange: (currentSelect, allSelected) => {
const result = allSelected.map(item => { return data.at(item.index) });
const selectedIds = result.map(item => {
return item.id;
});
console.log(selectedIds);
}

Trying to pass an 'id' of a traversed soundfile array to a play method

What I'd like to do is pass the id of the soundfiles array, that is iterated over in the Drum class, to the method play_pauseAudio(), so that each button plays one of nine soundfiles. I've got 'var myAudio = document.getElementById();' but don't know what to pass in that will refer to the id of each soundfile being mapped.
¬¬¬
import React from 'react';
import React from 'react';
class Drum extends React.Component {
super(props)
{
constructor(props)
}
play_pauseAudio = this.play_pauseAudio.bind(this);
play_pauseAudio()
{
var myAudio = document.getElementById();
myAudio.play();
}
render() {
console.log(this.props.id);
return this.props.soundfiles.map((eachfile) =>
<div>
<button onClick = {this.play_pauseAudio} >Play</button>
<audio controls source src = {eachfile.soundfileURL} style = {{width:0,height:0}} id = {eachfile.id}></audio>
</div>
)
}//render
}//class
export default Drum;
import React from 'react';
import './App.css';
import Drum from './components/Drum'
class App extends React.Component {
state =
{
sound_file_collection:
[
{
index:0,
soundfileURL:'https://s3.amazonaws.com/freecodecamp/drums/Heater-1.mp3',
letter:'Q',
description: 'PinkFloyd',
id: "audio_id0"
},
{
index:1,
soundfileURL:'https://s3.amazonaws.com/freecodecamp/drums/Heater-2.mp3',
letter:'W',
description: 'Handclap',
id: "audio_id1"
},
{
index:2,
soundfileURL:'https://s3.amazonaws.com/freecodecamp/drums/Heater-3.mp3',
letter: "E",
description: 'Cymbal',
id: "audio_id2"
},
{
index:3,
soundfileURL:'https://s3.amazonaws.com/freecodecamp/drums/Heater-4_1.mp3',
letter: 'A',
description:'AcousticGuitar',
id: "audio_id3"
},
{
index:4,
soundfileURL:'https://s3.amazonaws.com/freecodecamp/drums/Heater-6.mp3',
letter: 'S',
description:'GlamRock',
id: "audio_id4"
},
{
index:5,
soundfileURL:'https://s3.amazonaws.com/freecodecamp/drums/Dsc_Oh.mp3',
letter: 'D',
description: "Queen",
id: "audio_id5"
},
{
index:6,
soundfileURL:'https://s3.amazonaws.com/freecodecamp/drums/Kick_n_Hat.mp3',
letter: 'Z',
description: 'Tambourine',
id: "audio_id6"
},
{
index:7,
soundfileURL:'https://s3.amazonaws.com/freecodecamp/drums/RP4_KICK_1.mp3',
letter: 'X',
description: 'DampenedDrum',
id: "audio_id7"
},
{
index:8,
soundfileURL:'https://s3.amazonaws.com/freecodecamp/drums/Cev_H2.mp3',
letter: 'C',
description: 'MetalDrum',
id: "audio_id8"
}
]
}//end of state
render()
{
return (
<div className="App" id="drum-machine">
<Drum soundfiles = {this.state.sound_file_collection} />
</div>
);
}
}
export default App;
//Q, W, E, A, S, D, Z, X, C.
¬¬¬

Data does't appear in datatables

Data tidak tampil pada datatables
import React, { Component } from 'react';
import Linkify from 'react-linkify';
import './CobaData.css';
import './css/jquery.dataTables.css';
const $ = require('jquery')
$.Datatable = require('datatables.net')
export default class CobaData extends Component {
constructor(props) {
super(props);
}
render() {
let data = this.props.data;
let printData = data;
console.log(printData);
let table = $('#exc').dataTable(
{
data: printData,
columns: [
{ title: "ID" },
{ title: "Nama" },
{ title: "Nomor KTP" },
{ title: "Nomor HP" },
],
searchable: false,
orderable: false,
targets: 0,
}
);
console.log(table);
return (
<div>
<table className="display" width="100%" id="exc" >
</table>
</div>
);
}
}
data can't display datatables, when in console.log(printData) can be read in 'console'
before I used a normal table can be read on the table, but when using datatables on the data can not be read on the table
Using jQuery in a react app is not a good approach.
Instead I would recommend you to use react-data-table-component.
And then your Component would be like this:
import React, { Component } from 'react';
import DataTable from 'react-data-table-component';
import Linkify from 'react-linkify';
import './CobaData.css';
const columns = [
{
name: 'ID',
selector: 'id_karyawan'
},
{
name: 'Nama',
selector: 'nama'
},
{
name: 'Nomor KTP',
selector: 'KTP'
},
{
name: 'Nomor HP',
selector: 'no_hp'
},
];
export default class CobaData extends Component {
render() {
let data = this.props.data;
return (
<DataTable
title="Arnold Movies"
columns={columns}
data={data}
/>
);
}
}

Button not working for navigating to the other screen in react-native

I am using react-navigation and need by clicking the button sign in from welcomescreen move to signin page while its not working.
https://i.stack.imgur.com/w9vfX.png
Here is my code
1. app.js
StyleSheet, Text, View } from 'react-native';
import { TabNavigator, DrawerNavigator, StackNavigator } from 'react-navigation';
import WelcomeScreen from './screens/WelcomeScreen';
import SigninScreen from './screens/SigninScreen';
import SignupScreen from './screens/SignupScreen';
import HomeScreen from './screens/HomeScreen';
import BusinessScreen from './screens/BusinessScreen';
import TechScreen from './screens/TechScreen';
import EducationScreen from './screens/EducationScreen';
import LifestyleScreen from './screens/LifestyleScreen';
import ProfileScreen from './screens/ProfileScreen';
import FavoritesScreen from './screens/FavoritesScreen';
import SettingsScreen from './screens/SettingsScreen';
export default class App extends React.Component {
render() {
const MainNavigator = StackNavigator({
welcome: { screen: WelcomeScreen },
signin: { screen: SigninScreen },
signup: { screen: SignupScreen },
main: {
screen: DrawerNavigator({
home: { screen: HomeScreen },
business: { screen: BusinessScreen },
tech: { screen: TechScreen },
education: { screen: EducationScreen},
lifestyle: { screen: LifestyleScreen },
profile: {
screen: StackNavigator({
profile: { screen: ProfileScreen },
settings: { screen: SettingsScreen }
})
}
},
)
}
},)
return (
<MainNavigator />
);
}
}
2. WelcomeScreen
import React, { Component } from 'react';
import { View, Text} from 'react-native';
import Slides from '../components/Slides';
import PropTypes from 'prop-types';
const SLIDE_DATA = [
{ text: 'Welcome to JobApp', color: '#03A9F4' },
{ text: 'Use this to get a job', color: '#009688' },
{ text: 'Set your location, then swipe away', color: '#03A9F4' }
];
class WelcomeScreen extends Component {
render (){
onSlidesComplete = () => {
this.props.navigation.navigate('signin');
}
return (
<Slides data={SLIDE_DATA} onComplete={this.onSlidesComplete} />
);
}
}
export default WelcomeScreen;
3. Slide.js
import React, { Component } from 'react';
import { View, Text, ScrollView, Button, Dimensions } from 'react-native';
import PropTypes from 'prop-types';
const SCREEN_WIDTH = Dimensions.get('window').width;
class Slides extends Component {
renderLastSlide(index) {
if (index === this.props.data.length - 1) {
return (
<Button raised onPress={this.props.onComplete}
buttonStyle={styles.buttonStyle}
title="Sign In"
>
Sign In
</Button>
);
}
}
renderSlides() {
return this.props.data.map((slide, index) => {
return (
<View
key={slide.text}
style={[styles.slideStyle, { backgroundColor: slide.color }]}
>
<Text style={styles.textStyle}>{slide.text}</Text>
{this.renderLastSlide(index)}
</View>
);
});
}
render() {
return (
<ScrollView
horizontal
style={{ flex: 1 }}
pagingEnabled
>
{this.renderSlides()}
</ScrollView>
);
}
}
const styles = {
slideStyle: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
width: SCREEN_WIDTH
},
textStyle: {
fontSize: 30,
color: 'white'
},
buttonStyle: {
backgroundColor: '#0288D1',
marginTop: 15
}
};
export default Slides;
In WelcomeScreen if you are using arrow function like
onSlidesComplete = () => {
this.props.navigation.navigate('signin');
}
then you have to use the onPress like this.onSlidesComplete.bind(this) or if you are using the function like
onSlidesComplete() {
this.props.navigation.navigate('signin');
}
then you have to use the onPress like this.onSlidesComplete. In short replace the below code with your existing WelcomeScreen
import React, { Component } from 'react';
import { View, Text} from 'react-native';
import Slides from '../components/Slides';
import PropTypes from 'prop-types';
const SLIDE_DATA = [
{ text: 'Welcome to JobApp', color: '#03A9F4' },
{ text: 'Use this to get a job', color: '#009688' },
{ text: 'Set your location, then swipe away', color: '#03A9F4' }
];
class WelcomeScreen extends Component {
render (){
onSlidesComplete = () => {
this.props.navigation.navigate('signin');
}
return (
<Slides data={SLIDE_DATA} onComplete={this.onSlidesComplete.bind(this)} />
);
}
}
export default WelcomeScreen;

Resources